【发布时间】:2012-12-03 00:46:45
【问题描述】:
我有两种方法。一种是普通方法(MyMethod),一种是异步方法(MyMethodAsync)。我收到编译错误。
static string MyMethod()
{
var result = await MyMethodAsync(); // compile error here
return result;
}
async static Task<string> MyMethodAsync()
{
/** perform my logic here... **/
Thread.Sleep(1000);
return "yes";
}
错误信息是
“await”运算符只能在异步方法中使用。考虑使用 'async' 修饰符标记此方法并将其返回类型更改为 'Task'。
我很困惑。当我使用await 关键字时,调用线程将被挂起并等待任务完成。所以一旦await 被使用,该方法就不再是异步方法了。对吧?
备注:我知道我应该把逻辑放在MyMethod 和MyMethodAsync 调用MyMethod 来实现我想要的。
【问题讨论】:
标签: c# async-await