Call a Method Asynchronously in .NET using Polling Pattern and Callback Pattern
Posted by: Suprotim Agarwal
in Category .NET 4.0
Abstract: In my previous article, we explored multiple ways to call a method asynchronously using Delegates. In this article, we will see how to call a method asynchronously using the Delegate Polling pattern and the Callback Pattern.
In my previous article, Call a Method Asynchronously using Delegate BeginInvoke and EndInvoke Pattern we explored multiple ways to call a method asynchronously using Delegates. We learnt that we can call methods asynchronously in four different ways using the BeginInvoke() and EndInvoke() methods of the Delegate class. The four different ways are using the EndInvoke pattern, WaitHandle, Polling pattern and using a Callback Pattern.
In this article, we will see how to call a method asynchronously using the Delegate Polling pattern and the Callback Pattern. Make sure you have read my previous article to understand the BeginInvoke() and EndInvoke() methods of the Delegate class. This article assumes you have read and know these concepts.
Call a Method Asynchronously using Polling Pattern
In this pattern, the calling thread polls the other thread (doing async operation) periodically using the IAsyncResult object and checks whether the thread has completed. If not, it continues processing and checks the thread later. The application does not call EndInvoke() until it knows that the operation is complete. This pattern can be useful when you want your UI to be responsive, till the async operation completes. Let us see an example
In the code shown above, the thread initiates an async call and then polls the IAsyncResult.IsCompleted property to find out when the asynchronous call has completed. Till then it continues additional processing. EndInvoke is called only when IsCompleted returns True.
Call a Method Asynchronously using Callback Pattern
In this pattern, the initial thread initiates the async call but unlike the polling pattern, it does not wait or check to see if the thread that was called, has completed. Instead it makes use of a callback method. When the async method call completes, it makes use of this callback method to handle the results and call the delegate’s EndInvoke method. This pattern can be useful if you not want to process the results of the async call in the main thread. Let us see an example
As you can see, we have defined a callback method called CallbackMethod (you can call it anything you want) and supplied it to BeginInvoke. This callback method is nothing but a delegate of type AsyncCallback. We provide this callback delegate to the BeginInvoke() function and this callback delegate gets executed when the asynchronous call completes.
I hope you are beginning to see the difference between the callback pattern and the other patterns discussed previously. Here the initial thread (main method) does not wait or check to see if the async call has completed. Instead it makes use of this callback method which in turn gets executed when the asynchronous call completes. The callback method then calls EndInvoke() and processes the results of the asynchronous call and prints it on the console.
I hope you enjoyed reading this article and I thank you for viewing it.
The entire source code of this article can be downloaded over here