Async / Await Feature in .NET 4.5 Task Parallel Library (TPL)

Async / Await feature in .NET 4.5 Task Parallel Library (TPL)Current trends in the software development require the application to demonstrate fast reaction in response to the user’s actions. A modern application cannot just ‘hang’ when performing any complex operation and forces the user to wait. Moreover, the question of rational use of computing resources is still important. Since modern PCs are multi-core, it is possible to speed up the complex calculation processes by running them on multiple cores simultaneously. As a result, it is difficult to imagine up-to-date software applications that doesn’t use async operations.

Using async operations imposes some overhead on the programmer, as previously such operations were used only in the exceptional cases.

The Task Parallel Library (TPL) for .NET 4.5 immensely simplifies the programming of async operations by using keywords async and await. Due to this the developer doesn’t need to worry about:

  • Creating a new thread
  • Thread synchronization
  • Calls to UI thread
  • The implementation of safe cross-thread calls
  • Catch insidious bugs related to multi-threaded operations

TPL library effectively takes care of all those tasks.

Also async / await increases the readability of the code, because the code looks as if all operations were performed synchronously, i.e., easy to keep track of the sequence of operations.

Here is an example of the async / await usage:

// Three things to note in the signature: // – The method has an async modifier.

// – The return type is Task or Task<T>. (See “Return Types” section.)

// Here, it is Task<int> because the return statement returns an integer.

// – The method name ends in “Async.”

async Task<int> AccessTheWebAsync()


// You need to add a reference to System.Net.Http to declare client.

HttpClient client = new HttpClient();


// GetStringAsync returns a Task<string>. That means that when you await the

// task you’ll get a string (urlContents).

Task<string> getStringTask = client.GetStringAsync(“”);


// You can do work here that doesn’t rely on the string from GetStringAsync.



// 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.

string urlContents = await getStringTask;


// The return statement specifies an integer result.

// Any methods that are awaiting AccessTheWebAsync retrieve the length value.

return urlContents.Length;



See also

About the Author

Artem Groshevoi

: “I was always interested in technical things. My first programming languages was Pascal, Delphi, and FoxPro. At the University I learned C# development and started to work as a freelancer. I always liked back-end programming tasks with high-load and complex requirements. Optimization of legacy applications is also one of my strong sides. Currently I’m working with C#, WPF, Silvelright, SQL, ASP.NET, WinRT, VB.NET.
As a software engineer, I try to keep learning new things: languages (Ruby, JS), methodologies (TDD, BDD, Lean), and frameworks (Rails, SignalR). I’m a founder and contributor of several open-source projects. My hobby is learning computer algorithms on, riding bicycle, and photography.”

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>