使用最新CTP5与异步/等待关键字,我写了一些代码,这显然不能编译:如何编写简单的异步方法?
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();
}
}
什么是‘等待’操作只能在方法中使用或lambda标有”个理由'异步'修饰符错误?“ (我选择了Visual Studio指向的行)
*您可以*'声明作为Main''async'和'await'ing了什么东西* *会结束你的程序 – dlev
(假设你没有其他前台线程运行。)杜,我一直在想,这个错误在我的测试方法中。 Visual Studio应该对这个错误提供更多信息(比如“方法Main must async”) – ghord
在C#5版本中,如果将Main标记为async,则会出现以下错误:“入口点不能用'async 'modifier“ – Anthony