这是我的事件处理程序代码:
protected async void TestrunSaveExecute()
{
bool saveResult = await SaveTestRunAsync();
}
Run Code Online (Sandbox Code Playgroud)
为了保持UI响应,我使用了async
/ await
方法.
根据我的理解,我现在可以在SaveTestRunAsync()
不阻止UI的情况下进行一些冗长的操作,因为它通过使用await
关键字解耦.
private async Task<bool> SaveTestRunAsync()
{
//System.Threading.Thread.Sleep(5000); --> this blocks the UI
await Task.Delay(5000); // this doesn't block UI
return true;
}
Run Code Online (Sandbox Code Playgroud)
你能否解释为什么Thread.Sleep
仍然阻止用户界面的呼叫,而Task.Delay
不是?