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

2

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?

Archy answered 17/10, 2013 at 8:38 Comment(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".Thromboplastin
V
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.

Veg answered 17/10, 2013 at 8:41 Comment(12)
I am using VS2010. If I am not wrong, this is only for VS2012 and does not work with VS2010. isn't it?Handrail
@ÁlvaroGarcía, you can easily to try it.Oma
@Á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.Veg
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?Handrail
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.Handrail
@Á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?Veg
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.Handrail
@Á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.Veg
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.Multivalent
That nuget command is "Install-Package Microsoft.Bcl.Async" and here's the gallery page: nuget.org/packages/Microsoft.Bcl.AsyncMultivalent
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?Gonzalez
@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 :)Veg
S
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.

Sacroiliac answered 19/11, 2014 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.