C# 在 .net 4 上使用异步等待
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9110472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using async-await on .net 4
提问by CodesInChaos
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.
我目前正在开始创建一个应用程序,该应用程序将从 C# 5 的 async-await 特性中获益良多。但我不确定要使用哪个版本的 VS 和异步运行时。
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.
查看操作系统流行度图表,我需要再支持 Windows XP 三年左右。看起来 .net 4.5 只能在较新版本的 Windows 上运行,所以我需要针对 .net 4.0。开发机器使用 Windows 7,所以使用较新版本的 VS 不是问题。
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)
- VS2010 与 AsyncCTP
- VS2012 预览版(以及最终版),将目标设置为 .net 4.0
- Mono(看起来 2.12 有 async-await,我更喜欢/习惯使用 VS 而不是 MonoDevelop 作为 IDE)
Which one has fewer code-gen bugs? Looking at Jon Skeet's blogthe VS2012 Preview uses a never code-generator than the CTP.
哪个代码生成错误较少?查看Jon Skeet 的博客,VS2012 Preview 使用的代码生成器从未使用过 CTP。
And more importantly which runtime to use?
更重要的是要使用哪个运行时?
Does VS2012 contain a redistributable async runtime for use with .net 4?
VS2012 是否包含用于 .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.
通过引用 AsyncCTP 运行时,我设法编译了带有预览的代码。但由于 CTP 有奇怪的许可条件,这看起来不是一个好的长期解决方案。
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.
为了分发库,我更喜欢简单地将 dll 放在与应用程序相同的目录中,而不是某种安装程序。
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.
如果我的二进制文件可以在 mono+Linux/MacOS 上运行而无需更改,我也很喜欢。因此,运行时应该与内置的任何单声道(可能是 2.12)兼容,或者允许在非 Windows 操作系统上使用。
采纳答案by Omer Mor
Microsoft released the Async Targeting Pack (Microsoft.Bcl.Async)through Nugetas a replacement for the AsyncCTP.
Microsoft通过Nuget发布了Async Targeting Pack (Microsoft.Bcl.Async)作为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.
您可以在此处阅读更多相关信息: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.
您可以在此处阅读有关先前版本的信息: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.
由于此包得到官方支持,我现在相信针对 XP + async 的最佳选择是使用 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.
如果您觉得需要以 .NET 3.5 为目标,您仍然可以使用(我的)AsyncBridge for .NET 3.5。
回答by Alexey Raga
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.
如果您想在 MS 发布 C# 5.0 之后开始分发您的软件,那么您可以开始使用 AsycnCTP 进行开发。否则我不会推荐你使用它,因为它只是 CTP,甚至不是测试版。它可以在接近测试阶段和发布时进行很多更改。它可能不稳定等。
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.
如果你想在你的应用程序中引入简单的异步操作,我建议你使用 Reactive Extensions 和构建在顶部的东西(Reactive UI 等),它非常漂亮。
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.
至于 VS2012,它也包含相同的 Async CTP,就我记得的 //Build/ 平板电脑 MS 在那个会议上给我的。
回答by Justin
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.
如果您希望能够分发您的软件,我认为 Mono 解决方案确实是您现在唯一的选择。您还说您希望最终结果在 Linux 和 OS X 上的 Mono 上运行。首先以 Mono 为目标似乎是自然的解决方案。
Your next issue is the IDE. MonoDevelop would obviously work well but you say you prefer Visual Studio.
您的下一个问题是 IDE。MonoDevelop 显然运行良好,但您说您更喜欢 Visual Studio。
Greg Hurlman created a profileto 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.
Greg Hurlman创建了一个配置文件,用于从 Visual Studio 中针对 Mono 2.8 进行编码。如果您跟进他,他可能会为您指明在 Visual Studio 中针对 Mono 2.11/2.12 进行开发的正确方向。
Of course, there is also Mono Tools for Visual Studiowhich is a commercial product. I assume that it is still being offered by Xamarin.
当然,还有Mono Tools for Visual Studio,这是一个商业产品。我认为它仍然由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.
您也可以在 .NET 之上从 Mono 运行所需的 4.5 配置文件程序集,但我还没有尝试过。4.5 配置文件是 4.0 API 的严格超集。也许试一试并报告。
EDIT: It looks like perhaps you can use the Visual Studio Async CTP in production now
编辑:看起来您现在可以在生产中使用 Visual Studio Async CTP
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.
包括用于生产用途的新 EULA。注意 - 此许可证不鼓励您将 CTP 用于您的生产代码。CTP 仍然是不受支持且使用风险自负的技术预览。但是,我们收到了许多开发人员的请求,要求将 CTP 用于生产代码,因此更改了许可证以允许这样做。
回答by Marc Sigrist
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.
如果您愿意考虑其他 .Net 语言,F# 可以解决您的问题。它已经使用 async{} 计算表达式多年,并且即使与 .Net 2.0 也向后兼容。最低要求是 Windows XP SP3。运行时可以在这里下载。
回答by Alex Davies
It's possible to use the VS 12 beta to target .NET 4.0 using async/await.
可以使用 VS 12 beta 使用 async/await 来定位 .NET 4.0。
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
编辑:我们采用了这种技术并将其变成了一个名为 AsyncBridge 的开源库:https://nuget.org/packages/AsyncBridge

