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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19423251/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:56:56  来源:igfitidea点击:

async/await keywords not available in .net 4.0

c#async-await

提问by álvaro García

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.

我正在使用 VS2010 和 SP1。

Thanks.

谢谢。

采纳答案by Stephen Cleary

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 VS2012with Microsoft.Bcl.Async.

唯一支持的方法是将VS2012Microsoft.Bcl.Async.

VS2010 is very difficult to get working with 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. However, that package never worked well with the VS2010 updates. So, you'd have to first find a version of one of the old CTP installers, play around with installing someVS updates, and then see if the CTP works. If you've already installed all your VS2010 updates then no version of the CTP will work. I thinkonce you find an update situation where you can install a working CTP, thenyou can install the other updates.

VS2010 很难使用async/ await。有一个旧的 Async CTP 包(其中有许多从未修复的错误)充当 VS2010 的“附加组件”/“部分替换”。但是,该软件包在 VS2010 更新中一直无法正常工作。因此,您必须首先找到旧 CTP 安装程序之一的版本,安装一些VS 更新,然后查看 CTP 是否有效。如果您已经安装了所有 VS2010 更新,那么没有任何版本的 CTP 可以工作。我认为一旦您找到可以安装工作 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.Asyncand have a fully supported solution.

或者,您可以按照 Jon Skeet 的建议下载 VS2012 Express 的免费版本,Microsoft.Bcl.Async并获得完全支持的解决方案。

回答by Carlos Landeras

Async/Awaithave been introduced with C# 5.0and .NET Framework 4.5

Async/Await已经引入C# 5.0.NET Framework 4.5

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

在 C# 5.0 中使用 async 和 await 进行异步编程

If you are using Framework 4as I do in enterprise you can use other workarounds. You can use a NuGet package that allows you to use these features.

如果您像我在企业中那样使用Framework 4,则可以使用其他解决方法。您可以使用允许您使用这些功能的 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 库中提取:

This package enables Visual Studio 2012 projects to use the new 'async' and 'await' keywords. This package also includes Task-based extension methods that allow using some of the existing asynchronous APIs with the new language keywords. Windows Phone Silverlight 8 projects can use this package to get access to async extension methods for the networking types.

此包使 Visual Studio 2012 项目能够使用新的“async”和“await”关键字。这个包还包括基于任务的扩展方法,允许使用一些现有的异步 API 和新的语言关键字。Windows Phone Silverlight 8 项目可以使用此包来访问网络类型的异步扩展方法。

回答by johv

I have written a .NET 4.5 plugin using asyncfor 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. Then my plugin was loaded with reflection using Assembly.Load(...)or similar. I tried both async/awaitand Environment.CurrentManagedThreadId(a .NET 4.5 property), and both worked.

我认为这是有效的,因为我安装了 .NET 4.5,它用更新的运行时替换了 .NET 4 运行时,它同时用于 .NET 4.0 和 .NET 4.5。然后我的插件加载了反射使用Assembly.Load(...)或类似的。我尝试了async/awaitEnvironment.CurrentManagedThreadId(.NET 4.5 属性),并且都有效。

Thus if installing .NET 4.5 is an option (i.e. 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. 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 是一种选择(即不支持 Windows XP),则可以通过反射加载应用程序的 .NET 4.5 部分来混合使用,可能让您的 4.5 部分实现 4.0 接口。但是,如果您自己编写应用程序,那么更简单的解决方案是将整个项目切换到 .NET 4.5。

回答by Xtian11

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/