27

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?

solublefish
  • 1,681
  • 2
  • 18
  • 24
Álvaro García
  • 18,114
  • 30
  • 102
  • 193
  • 1
    Please reopen the question: question is "how can I use the async keywords in a project targetting .Net 4.0" which is not offtopic. This question is not "please recommend a library for task blah"; the question is "how can I achieve task blah". – MarkJ Apr 22 '14 at 09:59

2 Answers2

51

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.

DeepSpace101
  • 13,110
  • 9
  • 77
  • 127
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I am using VS2010. If I am not wrong, this is only for VS2012 and does not work with VS2010. isn't it? – Álvaro García Oct 17 '13 at 08:43
  • 1
    @ÁlvaroGarcía, you can easily to try it. – Hamlet Hakobyan Oct 17 '13 at 08:45
  • 3
    @ÁlvaroGarcía: Well the nuget package will probably install - but you won't be able to use async/await with it, because that's a *language* feature. While you *could* still the CTP, I'd strongly recommend that you don't - it has bugs. You should really bite the bullet and upgrade to VS2012. – Jon Skeet Oct 17 '13 at 08:50
  • I have installed the packages and there are a version for .NET 4.0, but the keyword async is not avaliable jet. I need to install any other packges? – Álvaro García Oct 17 '13 at 08:51
  • Well, by the moment is not for production application, more for tests, so if it has some bugs, is not a problem in this case. – Álvaro García Oct 17 '13 at 08:54
  • @ÁlvaroGarcía: Rather you than me, to be honest. You'll have to see if you can find an old CTP installer. But just bear in mind that you're fundamentally running unsupported software - if this is just for some tests, why not install Visual Studio 2012 Express just to try it out? – Jon Skeet Oct 17 '13 at 09:26
  • I can see in the blog: http://blogs.msdn.com/b/bclteam/archive/2013/04/17/microsoft-bcl-async-is-now-stable.aspx that this is a stable version, so for my case it is more that enough. – Álvaro García Oct 17 '13 at 09:29
  • 8
    @ÁlvaroGarcía: Yes, the package is stable, but you've missed the point - I don't believe it's going to change the *compiler*, which is what you need in order to use async/await. The package just provides the *libraries* which let you use async/await when targeting .NET 4 from VS2012. It's important to differentiate between library support and language support here. – Jon Skeet Oct 17 '13 at 09:31
  • This is working well for me but I thought I'd share a gotcha: be sure to change your project targets to 4.0 *before* installing via nuget. – solublefish Nov 03 '14 at 12:30
  • That nuget command is "Install-Package Microsoft.Bcl.Async" and here's the gallery page: http://www.nuget.org/packages/Microsoft.Bcl.Async/ – solublefish Nov 03 '14 at 12:31
  • Just to be clear, if you are using VS2012+, and are targeting net40, this package isn't needed as the newer VS includes a newer compiler? IOW, the framework version doesn't really matter? – Josh Jan 13 '22 at 18:43
  • @Josh: The package isn't about teaching the compiler new tricks. It provides the types that are required for the compiler to work with. Frankly, if you're still targeting .NET 4.0 (rather than 4.7.2 or whatever the latest supported version is) in 2022, there are bigger problems :) – Jon Skeet Jan 13 '22 at 19:27
1

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.

johv
  • 4,424
  • 3
  • 26
  • 42