更新时间:2021-07-17 09:37:52
但是问题在于,它在等待之后仍然在UI线程中运行.
But the problem is that it still runs in the UI thread after the await.
是的,会的.这是 async
/ await
的全部内容的一半-您可以返回到正确的上下文.
Yes, it would. That's half the whole point of async
/await
- that you get to come back to the right context.
听起来好像您真的应该启动一个新线程(或者***使用 Task< T>
)进行后台操作(如果无法避免)阻止IO.然后,您可以在异步方法中使用结果:
It sounds like you really should start a new thread (or preferably, use a Task<T>
) for the background operation, if you can't avoid the blocking IO. Then you can use the result in an async method:
async Task Foo(...)
{
string result = await Task.Run(...);
// Now update the UI with the result
}