Task.Delay永不完成 [英] Task.Delay never completing
问题描述
以下代码将永久冻结.
public async Task DoSomethingAsync()
{
await Task.Delay(2000);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
DoSomethingAsync().Wait();
// Task.Delay(2000).Wait();
}
如果我使用注释掉的代码将呼叫切换到DoSomethingAsync
,它的行为将与预期的一样.我怀疑嵌套等待会导致死锁,但是我不确定为什么,或者如何解决.
If I switch the call to DoSomethingAsync
with the commented out code, it behaves as expected. I suspect that somehow the nested awaits are causing a deadlock, but I'm not sure why, or how to fix it.
推荐答案
假定Button_Click
在GUI
线程中运行,则您手上会有死锁.
Assuming Button_Click
runs in the GUI
thread you have a deadlock on your hands.
在任务上使用Wait
时,您正在同步阻塞线程,直到任务结束,但是该任务永远不会结束,因为延续(Task.Delay(2000);
的完成)也必须在GUI线程上运行(在Wait).
When you use Wait
on a task you are synchronously blocking the thread until the task ends, but the task will never end because the continuation (the completion of Task.Delay(2000);
) must run on the GUI thread as well (which is blocked on Wait).
您有几种解决方案.使用ConfigureAwait(false)
都不捕获GUI线程的SynchronizationContext:
You have several solutions. Either use ConfigureAwait(false)
to not capture the GUI thread's SynchronizationContext:
public async Task DoSomethingAsync()
{
await Task.Delay(2000).ConfigureAwait(false);
}
或者(建议使用)async void
事件处理程序(这是async void
方法的唯一适当位置):
Or (which I recommend) use an async void
event handler (which is the only appropriate place for an async void
method):
private async void Button_Click(object sender, RoutedEventArgs e)
{
await DoSomethingAsync();
}
public async Task DoSomethingAsync()
{
await Task.Delay(2000);
}
这篇关于Task.Delay永不完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!