0

Its something around:

async Task foo(...)
{
   await Task.Yield();
   [long blocking I/O statements]
}

But the problem is that it still runs in the UI thread after the await. I made sure by getting the Thread.CurrentThread.ManagedThreadId for both the UI thread and the thread after await, and the UI thread is becoming not responsive.

So what should I do ? Should I use threads in the traditional way instead of relying on async/await ? or is there a way to make it run in a new non-UI thread after the await ?

4

1 回答 1

4

但问题是它在等待之后仍然在 UI 线程中运行。

是的,它会的。这是async/的一半await- 你可以回到正确的上下文。

听起来你真的应该为后台操作启动一个新线程(或者最好使用 a Task<T>),如果你不能避免阻塞 IO。然后您可以在异步方法中使用结果:

async Task Foo(...)
{
    string result = await Task.Run(...);
    // Now update the UI with the result
}
于 2012-10-25T14:40:37.447 回答