2
我偶然发现了异步等待的奇怪行为。异步等待的异步行为
示例代码:
public class foo
{
public async static Task<myobj> method1()
{
var result = await method2();
return result;
}
private async static Task<myobj> method2()
{
// omitted for brevity.
}
}
public class bar
{
public void caller()
{
var result = foo.method1().Result;
pass(result);
}
}
这冻结UI。解决方案是在caller()上实现异步等待。
但是这个怎么样:
public class foo
{
public static myobj method1()
{
var result = method2().Result;
return result;
}
private async static Task<myobj> method2()
{
// omitted for brevity.
}
}
public class bar
{
public void caller()
{
var result = foo.method1();
pass(result);
}
}
这种自由的工作。
私人电话与其他类别的上行方法有什么不同?
确切的情况在这里描述:http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html – Noseratio