Wednesday, 4 September 2013

Async and Await in .Net Framework 4.5

Asynchronous processes can improve the performance of application and make your application more responsive. It can be difficult to debug and maintain the asynchronous application.

But the latest .Net framework (4.5) provided Asyc & Await keyword which can make it easy to implement, debug & maintain asynchronous application with minimal required efforts.

Difference between Synchronous and Asynchronous Methods

Asynchronous processes does not depends on each other's output and they do run on different threads simultaneously, while synchronous processes are opposite kind of thing which wait for the previous process to be complete before starting the next process.



When Asynchronous should use

Asynchronous is essentially to use in application where there are chances for process to be blocked for such operation to be complete. In the case where your application access resources from web server and sometimes server response are slow or delayed, it increases the possibility of application to get blocked. In synchronous application when server respond is weak and its taking time then whole application has to wait for the respond and at that time user might think that application is blocked and he/she try to rerun it.

For same scenario in asynchronous application, the application can continue with other process which doesn't depend on the web resource until the blocking tasks get finished.

It is more effective to use Asynchronous methods in UI related application, because all UI related process share one thread and due to that single UI related process block can make whole application paralyze.

In this case when you use asynchronous methods, the application continues to respond UI related process. You can close the application if you don't want to wait for it to finish.

How to write Asynchronous method

The Async and Await keywords in Visual Basic and the async and await keywords used in C#  async programming. By using those two keywords, you can use resources in the .NET Framework. This method is as easily as you create synchronous method. To refer asynchronous method you just have to define it by using async and await.

Get Nuget Package for Asynchronous programming feature in .Net Framework


Initially Microsoft has released asynchronous programming feature for .Net framework 4.5 only. And recently they have provided Nuget package for .Net Framework 4.0 as well.

Run the following command in the Package Manager Console to install Async for .NET Framework 4, Silverlight 4 and 5, and Windows Phone 7.5 and 8,.

Install-Package Microsoft.Bcl.Async

This package is not supported in Visual Studio 2010 or below.

1 comment: