简体   繁体   English

async / await关键字在.net 4.0中不可用

[英]async/await keywords not available in .net 4.0

I would like to use the async/await in C# 4.0 and I have installed the following package: 我想在C#4.0中使用async / await,我已经安装了以下软件包:

http://www.nuget.org/packages/Microsoft.Bcl.Async/ http://www.nuget.org/packages/Microsoft.Bcl.Async/

The problem is that I do not have async/await keywords available. 问题是我没有async / await关键字可用。

I am using VS2010 with SP1. 我正在使用带有SP1的VS2010。

Thanks. 谢谢。

You're not going to get a better answer than Jon Skeet's . 你不会得到比Jon Skeet更好的答案。

The only supported way to do this is to use VS2012 with Microsoft.Bcl.Async . 唯一支持的方法是使用VS2012Microsoft.Bcl.Async

VS2010 is very difficult to get working with async / await . VS2010非常难以使用async / await There was an old Async CTP package (which had many bugs that were never fixed) that acted as an "add-on"/"partial replacement" to VS2010. 有一个旧的Async CTP软件包(它有许多永远不会修复的bug)作为VS2010的“附加”/“部分替换”。 However, that package never worked well with the VS2010 updates. 但是,该软件包在VS2010更新时从未运行良好。 So, you'd have to first find a version of one of the old CTP installers, play around with installing some VS updates, and then see if the CTP works. 因此,您必须先找到一个旧的CTP安装程序的版本,然后安装一些 VS更新,然后查看CTP是否正常工作。 If you've already installed all your VS2010 updates then no version of the CTP will work. 如果您已经安装了所有VS2010更新,则不会有任何版本的CTP。 I think once you find an update situation where you can install a working CTP, then you can install the other updates. 一旦你找到可以安装工作CTP的更新情况, 那么你可以安装其他更新。

After all this work, you'll still end up with a bug-ridden (and definitely unoptimized) implementation of async . 完成所有这些工作后,您仍然会遇到async错误(并且绝对未经优化)的实现。

Or , you can do as Jon Skeet suggested and download the free version of VS2012 Express with Microsoft.Bcl.Async and have a fully supported solution. 或者 ,您可以像Jon Skeet建议的那样,使用Microsoft.Bcl.Async下载VS2012 Express的免费版本,并提供完全支持的解决方案。

Async/Await have been introduced with C# 5.0 and .NET Framework 4.5 已经在C# 5.0.NET Framework 4.5引入了Async/Await

more information here: 更多信息:

Asynchronous Programming with Async and Await (C# and Visual Basic) 使用Async和Await进行异步编程(C#和Visual Basic)

Asynchronous Programming in C# 5.0 using async and await 使用async和await在C#5.0中进行异步编程

If you are using Framework 4 as I do in enterprise you can use other workarounds. 如果您像在企业中一样使用Framework 4 ,则可以使用其他解决方法。 You can use a NuGet package that allows you to use these features. 您可以使用允许您使用这些功能的NuGet包。

Using async/await without .NET Framework 4.5 在没有.NET Framework 4.5的情况下使用async / await

Just install it from NuGet package manager: 只需从NuGet包管理器安装它:

Install-Package Microsoft.Bcl.Async 安装包Microsoft.Bcl.Async

Extracted from NuGet Gallery : NuGet Gallery中提取:

This package enables Visual Studio 2012 projects to use the new 'async' and 'await' keywords. 此程序包使Visual Studio 2012项目能够使用新的“async”和“await”关键字。 This package also includes Task-based extension methods that allow using some of the existing asynchronous APIs with the new language keywords. 此包还包括基于任务的扩展方法,允许使用一些现有的异步API和新的语言关键字。 Windows Phone Silverlight 8 projects can use this package to get access to async extension methods for the networking types. Windows Phone Silverlight 8项目可以使用此程序包访问网络类型的异步扩展方法。

I have written a .NET 4.5 plugin using async for a .NET 4.0 application, and to my surprise this actually worked! 我已经使用async为.NET 4.0应用程序编写了一个.NET 4.5插件,令我惊讶的是这确实有效!

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. 我认为这很有效,因为我安装了.NET 4.5,它将.NET 4运行时替换为更新的.NET 4,它同时用于.NET 4.0和.NET 4.5。 Then my plugin was loaded with reflection using Assembly.Load(...) or similar. 然后我的插件使用Assembly.Load(...)或类似的方式加载反射。 I tried both async/await and Environment.CurrentManagedThreadId (a .NET 4.5 property), and both worked. 我尝试了async/awaitEnvironment.CurrentManagedThreadId (.NET 4.5属性),两者都有效。

Thus if installing .NET 4.5 is an option (ie not supporting Windows XP) one could possibly use a mix by loading the .NET 4.5 part of an application via reflection, possibly letting your 4.5 part implement a 4.0 interface. 因此,如果安装.NET 4.5是一个选项(即不支持Windows XP),可以通过反射加载应用程序的.NET 4.5部分来使用混合,可能让4.5部分实现4.0接口。 However, if you are writing the application yourself, then a much simpler solution would be to switch to .NET 4.5 for the whole project. 但是,如果您自己编写应用程序,那么更简单的解决方案是切换到整个项目的.NET 4.5。

using System.Threading.Tasks;

private void simpleMethod()
{
    var tsk = Task.Factory.StartNew(() => DoSomeWorkAsync());
    Task.WaitAll(tsk);
    DataTable table = tsk.Result;
}

It is important that the asynchronous method should not contain any method that affects form controls 异步方法不应包含任何影响表单控件的方法,这一点很重要

    private DataTable DoSomeWorkAsync()
    {           
        System.Data.DataTable table = new System.Data.DataTable();
        Thread.Sleep(4000); // Any long time process
        return table;
    }

More information: https://www.simplethread.com/net-40-and-systemthreadingtasks/ 更多信息: https//www.simplethread.com/net-40-and-systemthreadingtasks/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM