只需在下面的MainPage
中编写以下简单代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
// It is not recommended to call Wait() in a UI thread. I write this only for a test.
DoAsync().Wait();
// This line enters on 10.0.17134 / 10.0.16299
// and never enters on 10.0.17763.
}
private async Task DoAsync()
{
await ApplicationData.Current.LocalFolder.CreateFileAsync("walterlv", CreationCollisionOption.ReplaceExisting);
}
很常见,如果DoAsync
是真正的异步操作,则Wait()
将导致死锁。但是,如果运行Button_Click
,则会发现死锁不会发生。
尝试多次单击该按钮,您会发现在随机点击计数之后会发生死锁。
我也尝试过10.0.116299。
当您单击按钮时,死锁立即发生。
高于或低于10.0.17134的异步操作之间有什么区别?
通知:不建议在UI线程中调用Wait()
或Result
,但是我对它们之间的不同行为感到困惑,因此上面写了测试代码。