[C#] Multithreading in C# 3 [8] – Asynchronous Processing Model

By default, delegates work synchronously. But you can use them asynchronously too. You do not even need to work with any types of “System.Threading” namespace.

1. BeginInvoke() and EndInvoke()

  • Declare any delegate type
  • Create a delegate variable
  • Check the available methods of the variable using IntelliSense.
public delegate int MathOp(int x, int y);

public static class AsynchPattern
{
  public static void Test1()
  {
    MathOp addOp = ((x, y) => x + y);
    addOp.
  }
}

Check the signature of 2 methods: BeginInvoke() and EndInvoke()

public IAsyncResult BeginInvoke(int x, int y, AsyncCallback cb, object state);
public int EndInvoke(IAsyncResult result);

If you look at the documentation of “Delegate” or “MulticastDelegate“, “BeginInvoke()” and “EndInvoke()” method do not exist. So what are they?

When you create a delegate type, the compiler automatically adds these methods using the signature of the delegate method.

2. IAsyncResult Interface

The “System.IAsyncResult” interface represents the status of an asynchronous operation.

public interface IAsyncResult
{
  Object AsyncState { get; } // the last parameter of BeginInvoke()
  WaitHandle AsyncWaitHandle { get; } // to wait for an asynchronous operation to complete
  bool CompletedSynchronously { get; } // false for asynchronous operation
  bool IsCompleted { get; }
}

3. Invoking a Method Asynchronously

  • Call the “BeginInvoke()” method to start the asynchronous operation
  • Check the IAsyncResult.IsCompleted property is true
  • Get the result by calling the “EndInvoke()” method
IAsyncResult result = addOp.BeginInvoke(10, 20, null, null);
while (!result.IsCompleted)
{
  // Wait until the operation is completed
  System.Threading.Thread.Sleep(1000);
}
int answer = addOp.EndInvoke(result);
Console.WriteLine("{0} + {1} = {2}", 10, 20, answer);

Easy, right? But if we just need to wait until the operation ends what ,in the first place, do we need to use an asynchronously call?

4. AsyncCallback Delegate

The “BeginInvoke()” method has a “AsyncCallback” parameter. What is it for?

public delegate void AsyncCallback(IAsyncResult ar);

It is a delegate type that represents a method to be called when a corresponding asynchronous operation completes.

When the callback method is called, the “System.Runtime.Remoting.Messaging.AsyncResult” object is passed as an “IAsyncResult” argument.

public class AsyncResult : IAsyncResult
{
  public Object AsyncDelegate { get; }
}

By using the “AsyncDelegate”, you can get the result.

public static void Test1()
{
  MathOp addOp = ((x, y) => x + y);

  IAsyncResult result = addOp.BeginInvoke(10, 20,
    ar =>
    {
      AsyncResult aResult = ar as AsyncResult;
      MathOp op = aResult.AsyncDelegate as MathOp;
      int answer = addOp.EndInvoke(ar);
      Console.WriteLine("{0} + {1} = {2}", 10, 20, answer);
    },
    null);

  // Doing some work here
  System.Threading.Thread.Sleep(1000);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s