Asynchronous Programming in .NET

“Performance” is a term that is used a lot when talking about apps, but it’s actually a pretty vague term. There are at least two aspects of performance that most people think about: app launch time and throughput. Both of these can be measured and described with actual numbers. The true test of an app, however, is end-user perception.

Asynchronous programming is a means of parallel programming in which a unit of work runs separately from the main application thread and notifies the calling thread of its completion, failure or progress. You may be wondering when you should use asynchronous programming and what are its benefits and problem points.

The main benefits one can gain from using asynchronous programming are improved application performance and responsiveness. One particularly well suited application for the asynchronous pattern is providing a responsive UI in a client application while running a computationally or resource expensive operation.

Async and Await

The async and await keywords in C# are the heart of asynchronous programming. By using those two keywords, you can use resources in the .NET Framework or the Windows Runtime to create an asynchronous method almost as easily as you create a synchronous method. Asynchronous methods that you define by using async and await are referred to as async methods.

The following example shows an async method. Almost everything in the code should look completely familiar to you.

async Task AccessTheWebAsync()
{
    HttpClient client = new HttpClient();
    Task getStringTask = client.GetStringAsync("http://www.haidermrizvi.com");
    DoIndependentWork();
    string urlContents = await getStringTask;
    return urlContents.Length;
}

GetStringAsync returns a Task. That means that when you await the task you’ll get a string (urlContents).

The await operator suspends AccessTheWebAsync.

AccessTheWebAsync can’t continue until getStringTask is complete.
Meanwhile, control returns to the caller of AccessTheWebAsync.
Control resumes here when getStringTask is complete.
The await operator then retrieves the string result from getStringTask.

The following characteristics summarize what makes the previous example an async method.

  • The method signature includes an async modifier.
  • The name of an async method, by convention, ends with an “Async” suffix.
    • The return type is one of the following types:

    • Task if your method has a return statement in which the operand has type TResult.
    • Task if your method has no return statement or has a return statement with no operand.
      • The method usually includes at least one await expression, which marks a point where the method can’t continue until the awaited asynchronous operation is complete. In the meantime, the method is suspended, and control returns to the method’s caller. The next section of this topic illustrates what happens at the suspension point.

      Summary

      The new async and await keywords enable you to provide significant user experience improvements in your apps without much effort on your part. This term does not mean multithreaded code. By default, code written with async and await is single-threaded. But threaded code works well here.

Leave a Reply

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