【发布时间】: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 CTPb/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