async and await in C# 5

C# 5 comes with just a few new features and async is arguably the more important of those. I have started looking into it a couple of days ago and I thought I would share my thoughts on it here.

Asynchronous programming was touted was the next big thing for quite some time, but it didn’t hit mainstream in the .NET world till recently. Well, people were always using threads from .NET 1.0 but it was hard and error prone to write multi-threaded programs. Library support and tool support was lacking. The Task Parallel Library (TPL) introduced in .NET 4.0 was a step in the right direction. In this context, making asynchronous programming easy for C# programmers is probably the right thing to do as far as Microsoft is concerned.

Two new keywords have been introduced in C# 5.0 to make working with asynchronous code easier – async and await.

The async keywords is used to mark a method as asynchronous. It is required to use the await keyword inside that method. Note that just marking a method with async does not make it asynchronous. In face you can write fully synchronous code in a method marked as async, and the compiler will be ok with that.

        async void NothingAsynchronousHere()


            Console.WriteLine(“This method is executed synchronously”);


Actually, the compiler is not completely ok with that, when you compile the code you will get a warning:

“This async method lacks ‘await’ operators and will run synchronously. Consider using the ‘await’ operator to await non-blocking API calls, or ‘await Task.Run(…)’ to do CPU-bound work on a background thread.“

Now let us look at an async method which is actually asynchronous:

        async Task<string> GetWebpage(string url)


            WebClient client = newWebClient();

            var response = await client.DownloadStringTaskAsync(url);

            return response;


This method shows how the await keyword is used. This method looks like a synchronous method, but it actually executes in 2 phases. The calling thread executes upto client.DownloadStringTaskAsync() (which is an asynchronous method and it returns immediately). After that the thread immediately returns to the caller of GetWebpage. The caller can chose to wait (remember, GetWebpage returns a Task<>) or continue its own execution (and maybe later get the result of the download using the Result property of the Task<>). Once the download completes, the rest of the lines in the async method is executed. Usually these lines of code are executed on the same thread as the calling thread (which means you don’t have to worry about accessing UI controls if the async method was invoked using an UI thread) but there are some cases where it won’t be the case (an example being using a Threadpool thread).

Here are some things to remember. An async method can return only void, Task or Task<>. Otherwise the compiler will complain:

The return type of an async method must be void, Task or Task<T>

Another thing to keep in mind is that you can await only on methods that returns Task or Task<>. This means that you cannot await on WebClient.DownloadStringAsync() since its return type is void.

Leave a Comment

Your email address will not be published. Required fields are marked *