Using async-await on .net 4
Asked Answered
P

5

137

I'm currently starting to create an application that would profit a lot from C# 5's async-await feature. But I'm not sure which version of VS and of the async runtime to use.

Looking at OS popularity charts, I'll need to support Windows XP for another three years or so. It looks like .net 4.5 runs only on newer versions of Windows, so I need to target .net 4.0. The development machines use Windows 7, so using a newer version of VS is not a problem.

Now I need to first choose a compiler for doing this:

  • VS2010 with AsyncCTP
  • VS2012 Preview (and final once it arrives), setting the target to .net 4.0
  • Mono (Looks like 2.12 has async-await, I prefer/am used to VS over MonoDevelop as IDE)

Which one has fewer code-gen bugs? Looking at Jon Skeet's blog the VS2012 Preview uses a never code-generator than the CTP.

And more importantly which runtime to use?

Does VS2012 contain a redistributable async runtime for use with .net 4?

I managed to compile code, with the preview, by referencing the AsyncCTP runtime. But since the CTP has strange licensing conditions, that doesn't look like a good long term solution.

Or should I use a third party implementation? Perhaps mono has one?

For distributing the library I prefer simply putting the dll in the same directory as the application, instead of some kind of installer.

I'd also like it if my binaries would work without changes on mono+Linux/MacOS. So the runtime should either be compatible with whatever mono (2.12 probably) has built in, or allow use on non windows OSs.

Penult answered 2/2, 2012 at 10:11 Comment(3)
I do not think you will get far with a CTP version since you will not be allowed to redistribute anything which is part of a CTP with a commercial application. There are surely bugs lurking around and it is not optimized yet for performance. You will perhaps be faster developing it but your customers will not be happy to install beta software which could interfere with finally released versions.Nyeman
@Alois The later versions of the AsyncCTP allow redistribution. And the worst thing that can happen is my application breaks. It's not like it can interfere with other applications, so I don't see understand your concert over interference with the final version. Also part of my question is if there will ever be a final version that supports WinXP in the first place.Penult
The license clearly states (Async CTP 3) "1.a.ii. you agree to cease such use immediately upon notice from Microsoft;". I suspect this notice will come from MS when it is released. I am no lawyer but I am sure your legal department (if you have one) would love to hear your rationale how you want to get around this without breaking the license terms.Nyeman
F
106

Microsoft released the Async Targeting Pack (Microsoft.Bcl.Async) through Nuget as a replacement for the AsyncCTP.

You can read more about it here: http://blogs.msdn.com/b/bclteam/archive/2013/04/17/microsoft-bcl-async-is-now-stable.aspx.

You can read about the previous version here: http://blogs.msdn.com/b/lucian/archive/2012/04/24/async-targeting-pack.aspx.

As this pack is officially supported, I now believe the best option for targeting XP + async would be using Visual Studio 2012 + C#5 + Async Targeting Pack.

If you feel the need to target .NET 3.5 though, you can still use (my) AsyncBridge for .NET 3.5.

Fourflusher answered 27/4, 2012 at 4:14 Comment(4)
I can't find any reference to a license for your AsyncBridge ?Hedgerow
Check here: github.com/OmerMor/AsyncBridge/blob/master/src/AsyncBridge/…Fourflusher
Keep in mind that using async targeting pack on .NET 4.0 requires KB2468871 to be installed.Thereof
The KB2468871 was released in Jun 2011 (and 6 months later v2 was released), but it still may not be installed, so see how to check it in WiX - https://mcmap.net/q/24370/-detect-and-require-a-windows-qfe-patch-for-during-installation.Parchment
R
24

If you are open to considering other .Net languages, F# can solve your problem. It has had the async{} computation expression for years, and is backwards compatible even with .Net 2.0. Minimum requirement is Windows XP SP3. The runtime can be downloaded here.

Ruse answered 26/3, 2012 at 14:55 Comment(0)
Z
4

It's possible to use the VS 12 beta to target .NET 4.0 using async/await.

You need to copy some code into your project that provides the types that the compiler relies on.

Details here

Edit: we've taken this technique and turned it into a open source library called AsyncBridge: https://nuget.org/packages/AsyncBridge

Zoa answered 9/4, 2012 at 10:40 Comment(0)
P
3

If you want to be able to distribute your software, I think that the Mono solution is really your only option right now. You also say that you want the end result to run on Mono over Linux and OS X. Targeting Mono to begin with seems like the natural solution.

Your next issue is the IDE. MonoDevelop would obviously work well but you say you prefer Visual Studio.

Greg Hurlman created a profile to code against Mono 2.8 from Visual Studio. If you follow-up with him, he might be able to point you in the right direction for developing against Mono 2.11/2.12 in Visual Studio.

Of course, there is also Mono Tools for Visual Studio which is a commercial product. I assume that it is still being offered by Xamarin.

You might also be able to run the required 4.5 profile assemblies from Mono on top of .NET but I have not tried that. The 4.5 profile is a strict super-set of the 4.0 API. Perhaps give it a shot and report back.

EDIT: It looks like perhaps you can use the Visual Studio Async CTP in production now

Here is what it says on the download page:

Includes a new EULA for production use. Note - This license does not constitute encouragement for you to use the CTP for your production code. The CTP remains an unsupported and use-at-your-own-risk Technology Preview. However, we’ve received many requests from developers to use the CTP for production code, and so have changed the license to allow that.

Pronuba answered 8/2, 2012 at 7:59 Comment(3)
What I use in development is the minor issue. The major issue is what I should hand to my WinXP users. Do you suggest bundle mono 2.12 with my application?Penult
Yesterday I looked into the mono sources, and at least several of the core async classes(Async...Builder and ...Awaiter) are very hard to separate from the rest of mono. Currently I'm looking into re-implementing AsyncCtpLibrary, possibly borrowing a bit from mono.Penult
On redistributing AsyncCtpLibrary, I know that it is possible in principle, but for one the license contains a few strange clauses. But my main issue here is what happens in the long run. If it becomes unsupported, and nobody fixed bugs in it, that might be annoying.Penult
D
2

If you want to start distributing your software after MS releases C# 5.0, then you can start developing using AsycnCTP. Otherwise I wouldn't recommend you to use it, as it is just CTP, not even a beta. It can be changed a lot close to the beta stage and to the release. It may be unstable, etc.

If you want to introduce easy async operations in your application I would recommend you to use Reactive Extensions and stuff built on top (Reactive UI, etc), it is just beautiul.

As for VS2012, it also contains the same Async CTP as far as I remember from my //Build/ tablet MS gave me on that conference.

Dekaliter answered 6/2, 2012 at 11:9 Comment(1)
I don't care about waiting for VS2012 release. I expect VS2012 to be released before my software is out of alpha. But even once VS2012 is released, I don't want to target .net 4.5, since that doesn't seem to be available on WinXP. So the main issue is which async runtime to use on .net 4.Penult

© 2022 - 2024 — McMap. All rights reserved.