Call a Method Asynchronously in .NET using Polling Pattern and Callback Pattern

Posted by: Suprotim Agarwal , on 1/26/2011, in Category .NET Framework
Views: 61433
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

Polling Pattern

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.

Polling Pattern Output

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

Callback Pattern

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.

Callback Method

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.

Callback Pattern Output

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

Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, DNC .NET Magazine, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls. and is authoring another one at The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Martinq on Thursday, February 17, 2011 8:31 PM
Can this be used in a real world example, for example a web app? Seems like the callback happens on another thread and you cannot update the client response?
Comment posted by Robert Simpson on Thursday, May 26, 2011 8:41 AM
Dear Martinq,

Maybe you should check this page: http://www.voip-sip-sdk.com/p_130-how-to-build-an-autodialer-in-c-using-the-ozeki-voip-sip-softphone-source-code-voip.html

Good luck, Rob
Comment posted by Dean87 on Wednesday, June 22, 2011 7:33 AM
Wow, Rob, it was really a good idea! I've found other interesting things, too. Everyone, check this out: http://www.voip-sip-sdk.com/p_200-a-simple-text-to-speech-voice-generator-for-sip-voip-call-voip.html
Comment posted by vishwanatha Gowda on Wednesday, October 31, 2012 5:09 AM
Very good article to clear the concepts
Comment posted by Fortuner on Friday, September 20, 2013 2:16 AM
simple and clear

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel