我正在尝试找出下面代码的问题。我原以为使用async和await让我忘记GUI问题,例如某些长时间运行的代码阻塞主线程。
在我点击按钮后,GUI响应良好,直到调用longRunningMethod
,如下所示:
private async void openButton_Click(object sender, RoutedEventArgs e)
{
//doing some usual stuff before calling downloadFiles
Task<int> result = await longRunningMethod(); // async method
//at this point GUI becomes unresponsive
//I'm using the result here, so I can't proceed until the longRunningMethod finishes
}
在方法完成之前,我无法继续进行,因为我需要 result
。为什么这段代码会导致我的应用程序冻结?
longRunningMethod()
的内部可能通过不正确使用async/await
阻塞了 UI 线程。 - thumbmunkeyslongRunningMethod()
里面的内容吗? - thumbmunkeys