【问题标题】:Asynchronous Programming with Async and Await使用 Async 和 Await 进行异步编程
【发布时间】:2013-04-11 01:30:53
【问题描述】:

我正在浏览本教程,了解如何在 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;
}

【问题讨论】:

  • async - await 来自 .NET framework 4.5,而不是 4
  • @CuongLe, async/await 来自 C#-5.0 但我在 .NET 4.0 中使用它已安装 Async CTP b/c 我无法在 Windows XP 上安装 .NET-4.5(或 VS2012)( VS2010)。将其视为对真正 .NET-4.0 的扩展,而不是仅与 .NET-4.5 一起提供的扩展

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


【解决方案1】:

NuGet manager 中搜索 async 并安装 Microsoft Async 以运行 async/await代码在

【讨论】:

    【解决方案2】:

    您必须使用BCL.Async 库,如下所述: Using async without .net 4.5

    【讨论】:

    • 安装包 Microsoft.Bcl.Async –pre
    【解决方案3】:

    我的目标是 .NET 4.0 框架,但不确定是否有任何 需要额外的组件

    可以在 .NET 4.0 中运行 async/await 代码而无需安装 .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.0 的 .NET 4.5 不兼容。网络 4.5

    但是你的代码有语法错误

    AccessTheWebAsync() 方法声明中,你应该有返回类型Task&lt;string[]&gt;,而不是你的Task&lt;string&gt;,即你应该写:

    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。如果不安装 VS2012,他将无法使用 Async Targeting Pack 引用 "Async for .NET Framework 4, Silverlight 4 and 5, and Windows Phone 7.5 and 8 1.0.16",而后者在 Wondows XP 上根本不可能,Nuget 在 VS2010 中带来错误的包不兼容并且无法在未安装 .NET 4.5 的情况下在 .NET 4.0 上使用

    在可能的情况下检查了很多次

    【讨论】:

      猜你喜欢
      • 2016-12-04
      • 2015-03-16
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多