c# - 使用 Async 和 Await 进行异步编程

标签 c# .net-4.0 async-await c#-5.0 async-ctp

我正在浏览有关如何在 C# 中进行异步编程的教程,但遇到了一个我不确定如何解决的错误。这是链接:http://msdn.microsoft.com/en-us/library/hh191443.aspx错误是:

Cannot find all types required by the 'async' modifier.  
Are you targeting the wrong framework version, or missing a reference to an assembly?   

我的目标是 .NET 4.0 框架,但不确定是否需要任何其他程序集。

代码如下:

public async Task<string> AccessTheWebAsync(Class1 class1, Class2 class2)
{
  // GetStringAsync returns a Task<string>. That means that when you await the 
  // task you'll get a List<string> (urlContents).
  Task<string[]> listTask = GetList(class1);

  // send message task

  // You can do work here that doesn't rely on the string from GetStringAsync.
  //CompareService();

  // The await operator suspends AccessTheWebAsync. 
  //  - AccessTheWebAsync can't continue until getStringTask is complete. 
  //  - Meanwhile, control returns to the caller of AccessTheWebAsync. 
  //  - Control resumes here when getStringTask is complete.  
  //  - The await operator then retrieves the string result from getStringTask. 
  string[] listContents = await listTask;

  // The return statement specifies an integer result. 
  // Any methods that are awaiting AccessTheWebAsync retrieve the length value. 
  return listContents;
}

public Task<string[]> GetList(Class1 class1)
{
    var taskArray = Task<string[]>.Factory.StartNew(() => GenerateResults(class1));
    return taskArray;
}
public string[] GenerateResults(Class1 class1)
{
    string[] results = new string[2];
    results[1] = "";
    results[2] = "";
    return results;
}

最佳答案

I am targeting the .NET 4.0 framework and am unsure as to any additional assemblies required

可以运行 async/await .NET 4.0 中的代码,无需安装 .NET 4.5,having included or referenced AsyncCtpLibrary.dll from Async CTP .在 Windows XP 上安装 .NET 4.5 或 Visual Studio 2012 是不可能的,没有安装 .NET 4.5 的 .NET 4.0 与安装了 .NET 4.5 的 .NET 4.0 是不同的。
例如,阅读此讨论:

我不建议在没有 .NET 4.5 的机器上使用 Nuget 来获取 .NET 4.0 的扩展,因为它实际上为错误的 .NET 4.5 或 .NET 4.0 带来了兼容包,而 .NET 4.5 与没有 .NET 4.0 的不兼容。净 4.5

但是你的代码有语法错误

你应该有返回类型 Task<string[]> ,而不是你的 Task<string> , 在 AccessTheWebAsync()方法声明,即你应该写:

public async Task<string[]> AccessTheWebAsync(Class1 class1, Class2 class2)()  

代替

public async Task<string> AccessTheWebAsync(Class1 class1, Class2 class2)()

为了使此方法返回类型为 string[] 的值:

return listContents;//where listContents is declared as string[] type   

更新:
在我的 true .NET 4.0 (without .NET 4.5 and VS2012) Windows XP machine with Async CTP 上检查此更正后运行的 OP 代码

为什么我的回答被否决了?匿名...

很明显,如果 OP 提出这样的问题,他没有安装 .NET 4.5。他将无法使用 Async Targeting Pack引用 "Async for .NET Framework 4, Silverlight 4 and 5, and Windows Phone 7.5 and 8 1.0.16"如果不安装 VS2012,后者在 Wondows XP 上是根本不可能的,Nuget 在 VS2010 中带来错误的包不兼容,并且在没有安装 .NET 4.5 的情况下无法在 .NET 4.0 上使用

检查了很多次,在很多情况下

关于c# - 使用 Async 和 Await 进行异步编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16074401/

相关文章:

c# - 文件仅在第一个条件下写入

c# - 是否可以在 .NET 4.0 框架上使用 TLS1.2 发送 HttpWebRequest

c# - 派生方法比 C# 中的覆盖更强大?

c# - 等待/异步任务不等待

javascript - try/catch 中的异步/等待在等待解决后未显示处理逻辑中的错误

C# 定时器和网络调用并行

c# - 单击后按钮上的标签变得模糊,并在 WPF 中恢复动画

c# - 添加基于带有 SignedXml 类的 Id 属性的引用时出现“格式错误的引用元素”

C# 的 LINQ 用于在 ruby​​ 中等效的集合操作

asp.net - 如何将 Sharepoint 项目从 .net 3.5 更新到 4.0