MessageDialog task not shown if i use Task.Wait() instead of await

前端 未结 2 1301
迷失自我
迷失自我 2021-01-22 19:33

I don\'t know the difference beetween await a task and use task.Wait() but for the MessageDialog.ShowAsync method with the first it works but not with the second (while the two

相关标签:
2条回答
  • 2021-01-22 19:59

    Showing the dialog (and responding to button click in it) has to be done from the UI thread. But if you call Wait() on the UI thread, you're basically saying that nothing else can happen on that thread, until that Task completes. That's why the dialog can't be shown and that's also why your application freezes.

    So, the UI thread is waiting for the dialog, but the dialog is waiting for the UI thread, which is a classical deadlock. I believe using Wait() on the UI thread is the most common cause of deadlocks in C# 5 GUI applications.

    0 讨论(0)
  • 2021-01-22 20:14

    task.Wait() blocks until the task is complete, while await continues processing. My guess is that because the UI is blocked, the message dialog cannot appear.

    0 讨论(0)
提交回复
热议问题