Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Extension Me Async: Run a System.Action Instantiation Asynchronously

5.00/5 (2 votes)
11 Mar 2012CPOL2 min read 7.9K  
This article will briefly focus on the topic of Asynchronous Programming by extending System.Action delegates.

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:

C#
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:

C#
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 loose! Please take a look at the available PDF, “Threading in C#” by Joseph Albahari for a more detailed explanation of proper asynchronous approaches.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)