Home > Uncategorized > async await in .NET 4.0

async await in .NET 4.0

i think you are agree that async await in c# 5 is a really cool feature. I will explain in the future blog post more detail about this. The problem now how if you want to use this feature in .NET 4.0. Let say that you can’t change the production environment to the latest .NET framework (4.5).

Firstly I have already installed Visual Studio 2012 in my machine. I try to change the target framework to .NET 4

image

 

At first I’m very happy because the async keyword is appear in my editor. But the red squiggly wipe all the happines.

image

You can see a cause of the problem at error window. Seems there are a missing type or assembly.

image

 

The answer to our problem is :

image

 

You can gain the simplicity and beauty of asynchronous to make your apps performance better. 

Maybe you see there is no executable download in the previous link. That’s because you have to get the required library through Nuget.

image

Search “Microsoft.CompilerServices.AsyncTargetingPack” from nugget package manager. And install the package. That’s strange that the package is for Visual Studio 11. Well. Let’s try to give it a shoot.

 

image

 

After you install it the red squiggly disappear. You can dive into detail what’s contained in this package from object browser

image

There are a a lot of extensions method contain in AsyncCompatLibExtensions.

image

Then writes your codes.

image

Compile and run it asynchronously. If you see the IL with Diassembler you can see that our async await compiled into the same result as .NET 4.5. You should thanks the compiler baby. Smile

image

 

Cheers

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: