Call a Method Asynchronously using Delegate BeginInvoke and EndInvoke Pattern
Posted by: Suprotim Agarwal
in Category .NET Framework
Abstract: In this article, we will explore multiple ways to call a method asynchronously using Delegates. You can call methods asynchronously in four different ways using the BeginInvoke() and EndInvoke() methods of the Delegate class
In this article, we will explore multiple ways to call a method asynchronously using Delegates. You 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 the Delegate EndInvoke Pattern. In the subsequent articles, we will explore the Polling Pattern and Callback pattern.
A delegate is a special method that encapsulates a method and represents a method signature. From the main thread, when a method is invoked using a delegate, the Main thread has to wait until the method, that was invoked, has completed. This could prove expensive if your code is performing a slow operation like downloading a large file from your server.
Let us see an example of how a method is called synchronously using a Delegate:
As you can see, the Main method has to wait until the method SquareMethod completes. This is because SquareMethod was executed on the same thread, as that of the Main method.
However with a little effort, you can use delegates to call any method asynchronously (works when delegate has only one method in its invocation list). This is done using the BeginInvoke and EndInvoke methods of the Delegate class.
BeginInvoke, EndInvoke and IAsyncResult
The BeginInvoke method initiates the asynchronous call on a separate thread taken from the ThreadPool. It consists the same parameters required by the method (that you want to execute asynchronously) and consists two additional optional parameters called the callback parameter and the state parameter. The advantage of using BeginInvoke is that it returns immediately and does not wait for the asynchronous call to complete.
BeginInvoke returns a reference to an object implementing the IAsyncResult interface, which can be used to monitor the progress of the asynchronous call.
The EndInvoke method retrieves the results of the asynchronous call and releases the resource used by the thread. The parameters of EndInvoke include the out and ref parameters of the method that you want to execute asynchronously, plus a reference to the IAsyncResult returned by the BeginInvoke.
If the BeginInvoke() and EndInvoke() are not clear yet, worry not, as we will see an example shortly.
Call a Method Asynchronously using Delegate
Now that you know how the BeginInvoke and EndInvoke methods of the Delegate class work, let us see the different ways/patterns available to use BeginInvoke() and EndInvoke() to make asynchronous calls
EndInvoke Pattern - In this pattern, we use BeginInvoke from the Main thread to call the method, then do some processing on the main thread and then call EndInvoke(). This is useful when you want to have the calling thread to continue processing at the same time the asynchronous call is executing. Here the EndInvoke() does not return until the asynchronous call has completed. In other words, the main thread waits till the asynchronous operation is complete.
WaitHandle – Using this technique, the main method calls the method asynchronously and waits for a WaitHandle before it calls EndInvoke().
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.
Callback Pattern - In this pattern, the initial thread initiates the async call but does not wait or check to see if the thread that was called, has completed. 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 of calling a method asynchronously using the EndInvoke Pattern
As you can see, BeginInvoke() is used to initiate the asynchronous call, which returns immediately after the call is made, so the Main method can continue processing while the async operation executes. The BeginInvoke() returns an IAsyncResult object to the calling thread.
IAsyncResult asyncRes = sd.BeginInvoke(10, null, null);
We then use the EndInvoke() function to retrieve the results of the asynchronous call. The EndInvoke() has a parameter that is a reference to the IAsyncResult, returned by the BeginInvoke.
int res = sd.EndInvoke(asyncRes);
EndInvoke() uses this parameter to find the thread it refers to, which can be used to monitor the progress of the asynchronous call.
That’s it. We just made an asynchronous call to a method using a Delegate!
In the next article, I will show you how to use the Polling pattern and Callback pattern to perform async calls on methods.
I hope you liked the article and I thank you for viewing it. The entire source code of this article can be downloaded over here
References: Calling Synchronous Methods Asynchronously
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry
, DNC Magazine for Developers
. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls
and The Absolutely Awesome jQuery CookBook
Suprotim has received the prestigious Microsoft MVP award for Fifteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn