Introduction
Building a multithreaded application can be done in several ways includes:
- Asynchronous delegates calls.
- The BackgroundWorker component.
- The Thread class.
In this article we will cover the asynchronous delegates calls, if you want to know about other two types see my articles (Using the BackgroundWorker component, Building a multithreaded application using Thread calss).
This article assumes that you have a good understanding of the delegate type, if you don't, see my article Delegates in C#.
Asynchronous delegates calls
As you may know, we use the delegate to point to method in the application, which can be invoked at later time.
Example 1
namespace DelegateSynchronousCalls
{
public delegate int MyDelegate(int x);
public class MyClass
{
//A method to be invoke by the delegate
public int MyMethod(int x)
{
return x * x;
}
}
class Program
{
static void Main(string[] args)
{
MyClass myClass1 = new MyClass();
MyDelegate del = new MyDelegate(myClass1.MyMethod);
//invoke the method synchronously
int result = del(5);
//this text will not show till the first operation finish
Console.WriteLine("Proccessing operation...");
Console.WriteLine("Result is: {0}", result);
Console.ReadLine();
}
}
}
As you can see we used our delegate to invoke the method in the main thread of our application, but what if we have a long running proccess that will be executed when we invoke the method, for example:
public int MyMethod(int x)
{
//simulate a long running proccess
Thread.Sleep(10000);
return x*x;
}
We have now an operation that will take about 10 second to complete so our application will be not responsive and we can't execute any other operations till the first one finish.
So, what we can do to overcome this problem?
The answer is to use the delegate to invoke the method asynchronosly, but how we can do this?. The next example will show you how to use our delegate to make asynchronous call of the method.
Example 2
namespace DelegatesAsynchronousCalls
{
class Program
{
public delegate int MyDelegate(int x);
public class MyClass
{
//A method to be invoke by the delegate
public int MyMethod(int x)
{
//simulate a long running proccess
Thread.Sleep(10000);
return x * x;
}
}
static void Main(string[] args)
{
MyClass myClass1 = new MyClass();
MyDelegate del = new MyDelegate(myClass1.MyMethod);
//Invoke our method in another thread
IAsyncResult async = del.BeginInvoke(5, null, null);
//do something while MyMethod is executing.
Console.WriteLine("Proccessing operation...");
//recieve the results.
int result = del.EndInvoke(async);
Console.WriteLine("Result is: {0}", result);
Console.ReadLine();
}
}
}
As you can see, we defined the delegate as usual but we don't start invoking our method directly, we used our delegate to call BeginInvoke() method that returns an IAsyncResult object, then we submit this object to the EndInvoke() method then EndInvoke() provide the return value.(complex right !!?)
You may wonder now from where all this methods come from?
The answer is simple, when you define a delegate, a custom delegate class is generated and added to your assembly. this class include many method like Invoke(), BeginInvoke() and EndInvoke().
When you use your delegate to call a method you actualy call the Invoke() method. Invoke() method executes your method synchronously (on the main thread) as in example 1.
When you want to make an asynchronous call to your underlying method first you call BeginInvoke(). When you call BeginInvoke() your method is queued to start on another thread.
BeginInvoke() doesn't return the return value of the underlying method. Instead it returns an IAsyncResult object that you can use to determine when the asynchronous operation is complete.
To take your results, you submit the IAsyncResult object to the EndInvoke() method of the delegate. EndInvoke() waits for the operation to complete if it hasn't already finished and then provide the return value.

ShehzadPosted Aug 9, 2018, 2:49 AM
Thanks Amr Monjid,
Masthan RaoPosted Apr 9, 2018, 3:10 AM
Thanks for very good article on delegate basics. It helps in understanding how can we use delegates for Async process
shashiPosted Jun 26, 2017, 4:12 AM
Great article....helped a lot.
Ravi kumar MVSPosted May 12, 2014, 4:19 AM
Great explanation...
Spyros PonarisPosted Jul 23, 2012, 6:55 AM
I think the article is well written and show how to implement delegates . Nice article and very helpful .
aroshadwPosted Jun 15, 2012, 12:09 AM
good article! Thanks
Ravi PattemPosted Oct 19, 2011, 3:09 AM
Good Explanation. The Article helped me a Lot
Danny CrossleyPosted Oct 19, 2010, 3:29 PM
Wish there were more like it. You let me see the whole wood, not just the trees!
Koteswararao MallisettiPosted Sep 3, 2010, 7:02 AM
suppose i have a 3 fileWatcherConrols i bind the 3 controls events to corresponding event handlers suppose for all rename event one rename event handler how can we use this in this situation
danny drorPosted Sep 7, 2009, 5:47 AM
This article helped me alot with understanding how to use async webrequests, Thanks! ----------------------- Danny, Los Angeles Locksmith
Niladri BiswasPosted Apr 13, 2009, 2:12 PM
Thanks man...Many doubts of mine are cleared now. I am felling comfortable slowly in using delegates. Thanks buddy. Cheers!
Anthony WalshPosted Jan 18, 2009, 4:32 PM
Hi Amr, Thank you very much for this tutorial, I found it very helpful. Regards, Anthony (Ireland).