Asynchronous Calls using Delegates in C#

Asynchronous Delegate provide the ability to call a synchronous method in an asynchronous manner. When you call a delegate synchronously, the Invoke method calls the target method directly on the current thread. If the compiler supports asynchronous delegates, then it will generate the Invoke method and the BeginInvoke and EndInvoke methods.

If the BeginInvoke method is called, the common language runtime will queue the request and return immediately to the caller. The target method will be called on a thread from the thread pool. The original thread, which submitted the request, is free to continue executing in parallel to the target method, which is running on a thread pool thread. If a callback has been specified on the BeginInvoke, it will be called when the target method returns. In the callback, the EndInvoke method is used to obtain the return value and the in/out parameters. If the callback was not specified on the BeginInvoke, then EndInvoke can be used on the original thread that submitted a request.

The .NET Framework allows you to call any method asynchronously. Define a delegate with the same signature as the method you want to call; the common language runtime automatically defines BeginInvoke and EndInvoke methods for this delegate, with the appropriate signatures.

The BeginInvoke method is used to initiate the asynchronous call. It has the same parameters as the method you want to execute asynchronously, plus two additional parameters that will be described later. BeginInvoke returns immediately and does not wait for the asynchronous call to complete. BeginInvoke returns an
IasyncResult, which can be used to monitor the progress of the call.

The code in this topic demonstrates four common ways to use BeginInvoke and EndInvoke to make asynchronous calls. After calling BeginInvoke you can:

1. Do some work and then call EndInvoke to block until the call completes.


2. Obtain a WaitHandle using IAsyncResult. AsyncWaitHandle, use its WaitOne method to block execution until the WaitHandle is signaled, and then call EndInvoke.

3. Poll the IAsyncResult returned by BeginInvoke to determine when the asynchronous call has completed, and then call EndInvoke.

4. Pass a delegate for a callback method to BeginInvoke. The method is executed on a ThreadPool thread when the asynchronous call completes, and can call EndInvoke.

Always call EndInvoke after your asynchronous call completes.

Reference:

Asynchronous Programming Overview -
http://msdn.microsoft.com/en-us/library/2e08f6yc(VS.71).aspx

Asynchronous Delegates -
http://msdn.microsoft.com/en-us/library/22t547yb(VS.71).aspx

.NET Delegates: Making Asynchronous Method Calls in the .NET Environment -
http://msdn.microsoft.com/en-us/magazine/cc301332.aspx

How to call a Visual C# method asynchronously -
http://support.microsoft.com/kb/315582

Asynchronous Method Invocation By mikeperetz -
http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx