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

11,808

Solution 1

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.

Solution 2

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.

Share:
11,808

Related videos on Youtube

Álvaro García
Author by

Álvaro García

Updated on June 14, 2022

Comments

  • Álvaro García
    Álvaro García almost 2 years

    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?

    • MarkJ
      MarkJ about 10 years
      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".
  • Álvaro García
    Álvaro García over 10 years
    I am using VS2010. If I am not wrong, this is only for VS2012 and does not work with VS2010. isn't it?
  • Hamlet Hakobyan
    Hamlet Hakobyan over 10 years
    @ÁlvaroGarcía, you can easily to try it.
  • Jon Skeet
    Jon Skeet over 10 years
    @Á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.
  • Álvaro García
    Álvaro García over 10 years
    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
    Álvaro García over 10 years
    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.
  • Jon Skeet
    Jon Skeet over 10 years
    @Á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?
  • Álvaro García
    Álvaro García over 10 years
    I can see in the blog: blogs.msdn.com/b/bclteam/archive/2013/04/17/… that this is a stable version, so for my case it is more that enough.
  • Jon Skeet
    Jon Skeet over 10 years
    @Á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.
  • solublefish
    solublefish over 9 years
    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
    solublefish over 9 years
    That nuget command is "Install-Package Microsoft.Bcl.Async" and here's the gallery page: nuget.org/packages/Microsoft.Bcl.Async
  • Josh
    Josh over 2 years
    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?
  • Jon Skeet
    Jon Skeet over 2 years
    @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 :)