Task.Delay永不完成

编程入门 行业动态 更新时间:2024-10-25 06:22:11
本文介绍了Task.Delay永不完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下代码将永久冻结.

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永不完成

本文发布于:2023-11-08 16:16:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569860.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Task   Delay

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!