看起来UWP异步IO仅从10.0.17763开始才是真正的异步操作,但之前不是真正的异步操作。该怎么解释?

时间:2018-11-05 01:40:11

标签: uwp async-await deadlock windows-10-universal

只需在下面的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);
}

定位到10.0.17134或更低版本

很常见,如果DoAsync是真正的异步操作,则Wait()将导致死锁。但是,如果运行Button_Click,则会发现死锁不会发生。

尝试多次单击该按钮,您会发现在随机点击计数之后会发生死锁。

我也尝试过10.0.116299。

定位到10.0.17763

当您单击按钮时,死锁立即发生。

我的困惑

高于或低于10.0.17134的异步操作之间有什么区别?

通知:不建议在UI线程中调用Wait()Result,但是我对它们之间的不同行为感到困惑,因此上面写了测试代码。

0 个答案:

没有答案