Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the async keywords in a project targeting.net 4.0

I would like to use the async keywords in a project that is created in .net 4.0.

If I go to the nuget.org website and I look for "async", i many results, but mainly I get this:

Visual Studio Async CTP (Version 3, Unofficial) 0.3.0

AsyncAwaitCTP 1.0.0

Which is the differences between both of them?

like image 400
Álvaro García Avatar asked Oct 17 '13 08:10

Álvaro García


2 Answers

You want the Microsoft.Bcl.Async package. That's a properly released, non-CTP package that is stable. This also requires VS2012 since an updated compiler is needed to understand async and await.

In theory one could use older tools from VS2010 along with the older CTP library, I'd strongly recommend that you don't - it has bugs and the installation itself is a hit-or-miss.

like image 161
Jon Skeet Avatar answered Nov 17 '22 09:11

Jon Skeet


I have written a .NET 4.5 plugin using async for a .NET 4.0 application, and to my surprise this actually worked!

I think this worked because I have .NET 4.5 installed, which replaced the .NET 4 runtime with an updated one, which is used both for .NET 4.0 and .NET 4.5. Then my plugin was loaded with reflection using Assembly.Load(...) or similar. I tried both async/await and Environment.CurrentManagedThreadId (a .NET 4.5 property), and both worked.

like image 24
johv Avatar answered Nov 17 '22 10:11

johv