如何在Visual Studio 2010和.NET 4.0中使用异步?

我想为当前的VS 2010 .NET 4.0 C#项目添加异步支持

我已经找到:

  • Visual Studio异步CTP – http://www.microsoft.com/en-us/download/details.aspx?id=9983
  • Microsoft.Bcl.Async – https://nuget.org/packages/Microsoft.Bcl.Async

我甚至没有真正区别他们。

我安装了两个。 Visual Studio异步CTP(版本3),Microsoft.Bcl和Microsoft.Bcl.Async。 (也用于在Microsoft.Bcl中运行tools\portable-net40+sl4+win8+wp71\install.ps1

而且还是看不出任何效果。 同样的错误

 public async Task 

– >

 Error 37 The type or namespace name 'async' could not be found (are you missing a using directive or an assembly reference?) 

那真的是我应该如何使用这些东西?

我认为你不应该这样做。 Visual Studio 2010版本的async / await更像是一个预览而不是其他任何东西。 如果你想在真实的生产级代码中使用它,你肯定应该升级到Visual Studio 2012(或2013,如果你可以等一会儿)。

如果您在实际生产代码中不需要这个,因为某些原因需要Visual Studio Pro,并且只是在玩弄,您可以使用Visual Studio 2012 Express。

我正在研究类似的东西(我正在VS2010中编写一个RestApiLibrary,我从VS2017项目中借用)并发现以下URL有用。

https://code.msdn.microsoft.com/Introduction-to-HttpClient-4a2d9cee

主要的帮助是:

  // Send a request asynchronously continue when complete client.GetAsync(_address).ContinueWith( (requestTask) => { // Get HTTP response from completed task. HttpResponseMessage response = requestTask.Result; // Check that response was successful or throw exception response.EnsureSuccessStatusCode(); 

‘ContinueWith’方法和’.Result’属性似乎是VS2010中异步函数(使用)的关键。 注意,我怀疑这种行为是以传统的异步方式进行的,但至少这种方式可以在VS2010中使用异步方法。

希望这可以帮助!