Asynchronous Programming in C# – async/await


Asynchronous Programming in C# – async/wait

Hi Folks,

I want to share with you an important enhancement in .NET framework 4.5. The asynchronous programming paradigm for .NET developers using new C# keywords async/await. Since I’m C# developer; I will be highlighting this new design pattern keywords in C# for asynchronous programming which is async/await which is supported only starting from .NET framework 4.5.

what’s async/await for C#?

Async is a new modifier in C# 4.5. async specifies to the compiler that the method is executing in asynchronous mode and not synchronous.

For example – C# Code:

async Task Sum(int x, int y) {
// your method implementation.
}

The async method returns a Task object or doesn’t return any objects and this case you will write void instead of Task object.

The async method doesn’t allow input parameters by reference or output.

The  async method has to contain a line to call the async method by using await operator.

For example – C# Code:
private async void btnAdd_Click(object sender, RoutedEventArgs e)
{
     int x=10,y=15,z=0;
    // The complier will call Sum asynchronously and return the value in z.
    z=await Sum(x,y);
 }


An async method provides a convenient way to perform potentially long-running processing without blocking the caller’s thread.

The caller of an async method can resume its work without waiting for the async method to finish which makes your application more responsive and user friendly.


Read more about async/await from MSDN:
http://msdn.microsoft.com/en-us/library/hh156513(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/hh156528.aspx

Hope this helps.

-ME



 

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>