【发布时间】:2011-07-21 07:42:59
【问题描述】:
使用最新的 CTP5 和 async/await 关键字,我写了一些代码,显然无法编译:
class Program
{
public class MyClass
{
async public Task<int> Test()
{
var result = await TaskEx.Run(() =>
{
Thread.Sleep(3000);
return 3;
});
return result;
}
}
static void Main(string[] args)
{
var myClass = new MyClass();
//The 'await' operator can only be used in a method or lambda marked with the 'async' modifier error ??!!
int result = await myClass.Test();
Console.ReadLine();
}
}
“'await' 运算符只能用在标有 'async' 修饰符错误的方法或 lambda 中”的原因是什么? (我选择了 Visual Studio 指向我的行)
【问题讨论】: