Here is the second article in the Extension Me series. The Extension Me series focuses on various uses of Extension Methods in .NET and primarily uses C# as the language of choice. This article will briefly focus on the topic of Asynchronous Programming by extending System.Action delegates. Using the extension method, any code wrapped in an instantiation of System.Action delegates can be executed asynchronously. The assumptions made about the reader are: has experience with the basics of programming in C# and knows what extension methods are. Please be advised: the implications of asynchronous programming are not to be ignored for severe consequences can occur.
Not only is asynchronous programming fun and beneficial (if used correctly), it is becoming an essential skill as Windows 8 is introduced. Although the implementation of the following asynchronous pattern is incompatible with Metro-style WinRT apps, the theory of asynchronous programming will surely be applicable. More information on Asynchronous Programming can be found at Visual Studio Asynchronous Programming where How-To videos, whitepapers, samples, and walkthroughs are available.
Below is the implementation of the extension method:
public static class ActionExtensions { public static void Async(this Action @this) { var thread = new Thread(@this.Invoke); thread.Start(); } }
The code snippet above shows how extension methods can introduce the beauty of System.Action and System.Threading.Thread objects working together. The Action and the Thread are very close. An essential function of System.Action is wrapping a block of executable code while an essential function of System.Threading.Thread is wrapping a block of execution. By embracing these two abilities, code can be executed asynchronously just by wrapping it in an Action delegate. Here is an example:
public void ReturnsQuick() { new Action(() => { //Long Running Code here runs on a separate thread… }).Async(); }
When the ReturnsQuick method is called, it creates a new instance of an Action passing a lambda containing “long-running code”. Using the extension method created previously, the code is executed on a separate thread. The code is now asynchronous and, as the name suggests, the ReturnsQuick method returns immediately after calling Async().
As mentioned above when introducing the topic of this article, there are plenty of caveats that must be addressed before adopting this approach (especially application-wide). Shared resource is one of the most important things to consider. If two threads try to access a single resource at the same time, one of them has to lose! Please take a look at the available pdf, “Threading in C#” by Joseph Albahari for a more detailed explanation of proper asynchronous approaches.
Great post, you have pointed out some good points, I as well think this is a very excellent website.
Thank you very much Delosantos. Stay tuned for my next post on creating a custom data-bound control for ASP.Net.
It’s in reality a nice and helpful piece of information. I’m glad that you just shared this useful info with us. Please keep us up to date like this. Thanks for sharing.
Thank you very much. Please see my other Extension Me entries.