Saturday, February 09, 2008

Asynchronous Programming in .NET (C#)

Asynchronous Programming is supported by several areas in the CLR like Sockets, Remoting, Web Services, File IO, etc. In order to fully take advantage of these features, I think, one should know how to make asynchronous method calls, in the first place.
Asynchronous Calls -> You invoke the method and do not wait for it to complete. Instead you go ahead executing the subsequent calls. This is advantageous for situations where the subsequent code has got nothing to do with this method call. For example, you have obtained some data which you wish to write to a socket. In this situation you might not have to wait until the data is written. In this case, you can write the data asynchronously. Refer to this article for more detailed explanation of APM (Asynchronous Programming Model)
In order to make asynchronous calls, follow the steps...

1. Create the method you wish to call asynchronously.
public static void HelloWorld(string message)
{
Console.WriteLine("Welcome to my blog : "+message);
}
2. Write a delegate with the same signature as that of your method. Note delegates are declared outside a class or a struct, but not within.

public delegate void AsyncHelloWorld(string message);
3. Within Main() or wherever you wish to make this call, create an instance of this delegate passing the method you wish to be invoked.

AsyncHelloWorld acm = new AsyncHelloWorld(HelloWorld);
4. Invoke the method using BeginInvoke() which returns an instance of IAsyncResult. The first parameter is the parameter that is to be passed to the method HelloWorld. If there are more than one parameters that the method takes, then pass them in the same order. So we should say the BeginInvoke() takes a variable set of parameters and except the last two, the rest of the parameters are passed to the method. The last two parameters are the AsyncCallBack (you pass a method that is to be invoked on completion of execution of this method) and AsyncState. More about the arguments is here.
IAsyncResult res = acm.BeginInvoke("Krishna",null,null);
5. This call does not block unlike other method calls and instead proceeds further with execution. Now you might at sometime require to wait until this method is actually executed. In that case you make use of EndInvoke() method which takes in argument as the IAsyncResult object returned by the BeginInvoke().
acm.EndInvoke(res); //wait until the method is completed
6. On completion of execution, this method returns.

The complete code is shown below and is written in Snippet compiler
using System;
using System.Collections.Generic;
//should import the following namespaces...Important!
using System.Runtime.InteropServices;
using System.Threading;

public delegate void AsyncHelloWorld(string message);
public class MyClass
{
public static void HelloWorld(string message)
{
Console.WriteLine("Welcome to my blog : "+message);
}


public static void Main()
{
Console.WriteLine("Method to be called");
AsyncHelloWorld acm = new AsyncHelloWorld(HelloWorld);
IAsyncResult res = acm.BeginInvoke("Krishna",null,null);
//HelloWorld("Krishna");
Console.WriteLine("Method has returned");
acm.EndInvoke(res);
}
}


Asycnhronous Programming Screencast ....
[Developed with DeBugMode Wink]

No comments: