Thursday, February 16, 2006

Asynchronous Programming - Delegates : Always a problem for 90% of .NET developers

Most of us when working with GUI applications has definitely encountered certain problems with updated refresh of view. I mean to say that if you perform a task which takes atleast 10 seconds odd to finish, and then you try to switch between windows and attempt to get back to the main application window, you can notice that the application has freezed. You have numerous options to overcome this. the simplest one is write a void method/sub procedure and create a thread that executes that sub procedure. Inside the sub procedure, you can call the Refresh() or Update() method of the form you wish to refresh the view. Well this topic is not about how to use threads to make Windows applicaitons free of freezing effects, but is about asynchronous programming.

Asynchronous method call is where we call the method and proceed to execute the next line after the call. We do not wait for the method to return. So how will you know when your method has finished execution? what if you want to return some information or a value once the method has been computed? As an answer to all these questions, there is the CALL BACK function. The call back function is that function that is executed when the method that has been invoked asynchronously has returned(completed execution) The call back function is passed as a delegate and so is the function that is being called. So learning how delegates work is the first step for one who wish to use asynchronous calls to functions. Almost all tasks which takes considerably good amount of time to execute have asynchronous versions, already provided. For example on a stream you have a BeginRead() method. All the methods that are asynchronous starts with "Begin" and has a corresponding "End" method which is actually a call back method.

First of all. Let us look at working with delegates in simple terms. I am no expert and this is no documentation. So spare me if you dont understand and go join some coaching classes.

Basic Signature
public delegate [returntype] [function-name] ([parameters]) //C# version
public delegate
[function-name] ([parameters]) as [returntype] ' Vb.NET version

For example
public delegate int MathOperation(int a,int b);

We have declared a delegate. delegates could be treated as objects, just that they can call only one method. Just follow the steps you shall understand.

Step 1
1. Declare a delegate, usually outside the class as shown
public delegate int MathOperation(int a,int b);
2. You need to prepare a method same as the delegate signature. For example, for MathOperation a valid method would be as
public int Addition(int a, int b)
{
return a+b;
}
3. You need to then create an instance of delegate passing the method name that it is supposed to call. Here,
public static void Main(string[] args)
{
//create an instance of delegate
MathOperation add=new MathOperation(Addition);
Console.WriteLine(add(10,20));
}
As you can see you invoke the method through delegate instance as shown [ add(10,20) ] It appears as if we have given a new name to the method instead of using the exisiting name. So what is the use of delegates when i could do the same without using delegates.

Usage??
The need for delegates rises in the situtaion where there are two objects say obj1 and obj2. Obj1 has a reference to obj2, so obj1 can invoke any method on the obj2b, but what if obj2 needs to invoke a method of obj1 whose reference it doesnt have. In that situation obj2 would provide a method to which obj1 can pass a delegate which holds the reference to the method of obj1 which obj2 needs to invoke. Inside the method of obj2, the delegate is registered with the obj2, so it is not the object reference that obj1 has but direct link to method of obj1. I hope you understand this. If you dont, then read again :))

[to be continued .....
[Next ... how to perform asynchronous method calls?]

No comments: