我以为还在继续同一个帖子 [英] I thought await continued on the same thread

查看:85
本文介绍了我以为还在继续同一个帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为关于async / await的一点是,当任务完成时,继续在调用await时在相同的上下文上运行,在我的例子中,这将是UI线程。



例如:



I thought one of the points about async/await is that when the task completes, the continuation is run on the same context when the await was called, which would, in my case, be the UI thread.

So for example:

System.Diagnostics.Debug.WriteLine("2: Thread ID: " + Thread.CurrentThread.ManagedThreadId);
await fs.ReadAsync(data, 0, (int)fs.Length);
System.Diagnostics.Debug.WriteLine("3: Thread ID: " + Thread.CurrentThread.ManagedThreadId);





我不希望这样:





I would NOT expect this:

2: Thread ID: 10
3: Thread ID: 11





给出了什么?为什么延续的线程ID与UI线程不同?



根据这篇文章 [ ^ ]我需要显式调用ConfigureAwait来改变连续上下文的行为!



Marc



What gives? Why is the thread ID for the continuation different than the UI thread?

According to this article[^] I would need to explicitly call ConfigureAwait to change the behavior of the continuation context!

Marc

推荐答案

结果是在UI启动和运行之前我正在调用异步读取,因此没有连续上下文!
Turns out I was calling the async read before the UI was up and running, so there was no continuation context!


这篇关于我以为还在继续同一个帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆