后台工作未在 GUI 线程上完成
我有一个在主线程上启动的后台工作程序,如图所示
(来源:developingtrends.co.uk)
它执行按预期在工作线程上
(来源:developingtrends.co.uk)
但是对于出于某种原因,它在工作线程上完成,如果我尝试更新 gui 线程上的任何内容,这会导致我出现问题。
(来源:developingtrends.co.uk)
我有在测试应用程序上尝试了简化的设置,在此应用程序中,线程确实在主线程上正确结束
(来源:developingtrends.co.uk)
你可以吗想到这可能发生的任何原因吗?
谢谢罗斯
I have a background worker that is started on the main thread as shown
(source: developingtrends.co.uk)
It executes on a worker thread as expected
(source: developingtrends.co.uk)
but for some reason it completes on the worker thread which causes me issues if I try and update anything on the gui thread.
(source: developingtrends.co.uk)
I have tried a simplified setup on a test app and in this app the thread does end correctly on the main thread
(source: developingtrends.co.uk)
Can you think of any reason this might have happened?
Thanks
Ross
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在旧版本的 Windows 窗体中可以看到此行为。即使 BGW 是在主线程上启动的,
SynchronizationContext.Current
的值为null
。这个错误在去年的某个时候得到了修复。来自我的博客:
This behavior was seen in older versions of Windows Forms. Even though the BGW is started on the main thread, the value of
SynchronizationContext.Current
wasnull
. This bug was fixed sometime in the last year.From my blog:
不知道为什么会发生这种情况。但如果您想处理结果,请尝试以下操作:
No idea why this happeneds. But if you want to process the result try the following: