Microsoft's new way with parallel code

### New Book Reviews!

 Microsoft's new way with parallel code
Friday, 29 October 2010

Microsoft has introduced a new set of features to .NET - both the C# and VB languages have been modified to make parallel asynchronous programming and in particular working with the UI thread easier - but the way that it works can be subtle and you need to understand it to see what you are getting.

The new asynchronous method facilities are powerful and promise to provide a solution to the long-standing problem of moving work off the UI thread without complicating the structure of your program.

Two new keywords do most of the work. The async modifier is used to create asynchronous methods as opposed to standard synchronous methods. An asynchronous method can be suspended in its execution by the occurrence of an await instruction. While suspended the method doesn't waste the resources of a thread and indeed releases the thread it was started on to do useful work.

For example:

async void MyMethod(int i){}

is an asynchronous method that may be suspended as part of its normal execution by an await command.

Asynchronous methods can return only void, Task or Task<T> types. The simplest case is when the asynchronous method returns void because we don't have to worry about retrieving a result when it is finally complete.

To suspend a synchronous method while another job is performed you use the await keyword.

The basic syntax is:

await t

where t is a method or "task" with some standard methods defined that allow it to be used in the await pattern - GetAwaiter, BegnAwait and EndAwait.

This is very similar to the asychronous invoke pattern implemented using BeginInvoke, EndInvoke but now with built in language facilities.

That is, these methods are used automatically by the await command to suspend the current asynchronous task, run t and and handle the return data and continuation of the original asynchronous task when t has terminated. Of course t is usually run on a thread of its own.

For example suppose MyAwaitableMethod() satisfies the requirements of the await pattern then you can write:
async void MyMethod(int i){ do things await MyObject.MyAwaitableMethod(); do more things}

In this case calling MyMethod would result in it doing things using the calling thread and then waiting, i.e. entering a suspended state until MyAwaitableMethod has completed when it then executes do more things using the original thread.

At this point you may be mystified as to what this construction is giving you that is new as the call to MyAwaitableMethod looks like a standard blocking method call. It isn't. This is because the calling thread is released by the await with the result that it looks as if MyMethod has executed a return. The crucial points are that MyAwaitableMethod has to be implemented to do its work on a different thread and the execution of MyMethod only continues when it has finished and on the original thread.

Notice that the the MyAwaitableMethod isn't automatically run on a new thread, simply using await or async don't automatically do anything with threading - you have to do the implementation manually. However for the user of the non-blocking MyAwaitableMethod everything is much simpler. There are lots of standard non-blocking methods that can be called using await that are hence much easier to use.

Of course this is an ideal approach to not blocking the UI thread - a standard problem in both Windows forms and WPF. Imagine that MyMethod was called as the result of a button click. As soon as we reach the await the UI thread returns and continues to process user interactions only to return to MyMethod after MyAwaitableMethod has completed.

This is a powerful approach capable of simplifying your code - and especially solving the long standing problem of shifting work off the UI thread.

Things become a little more complicated when the task has to return a result and the asynchronous method can have more than one await instruction, so grabbing and releasing the original thread multiple times. Once you understand how it all works it is remarkably simple.

See our tutorial on the new Async features.

If you would like to be informed about new articles on I Programmer you can either follow us on Twitter, on Facebook , on Digg or you can subscribe to our weekly newsletter.