【发布时间】:2012-09-06 07:54:41
【问题描述】:
在 C#/XAML 中的 Windows 8 应用程序中,我有时想从非异步方法调用可等待的方法。
其实替换这个是正确的:
public async Task<string> MyCallingMethod()
{
string result = await myMethodAsync();
return result;
}
通过这个:
public string MyCallingMethod()
{
Task.Run(async () => {
string result = await myMethodAsync();
return result;
});
}
对我来说的好处是我可以在没有 await 的情况下使用 MyCallingMethod,但这是正确的吗? 如果我想为 MyCallingMethod 传递 ref 参数,这可能是一个优势,因为异步方法中不可能有 ref 参数。
【问题讨论】:
-
行不通。您的匿名异步函数不会将值返回给外部函数。
标签: c# windows-8 async-await