问题描述
我的C#应用程序有几个背景的工人。有时候,一个后台工作人员会火了另一个。当第一个后台工作完成和 RunWorkerCompleted
事件被激发,哪个线程将这一事件发生火灾,UI还是先后台工作从的RunWorkerAsync
被称为?我使用微软的Visual C#2008例preSS版。如果您有任何想法或建议将是AP preciated。谢谢你。
My C# application has several background workers. Sometimes one background worker will fire off another. When the first background worker completes and the RunWorkerCompleted
event is fired, on which thread will that event fire, the UI or the first background worker from which RunWorkerAsync
was called? I am using Microsoft Visual C# 2008 Express Edition. Any thoughts or suggestions you may have would be appreciated. Thanks.
推荐答案
如果在的BackgroundWorker
从UI线程的创建,那么 RunWorkerCompleted
赛事也将提高在UI线程。
If the BackgroundWorker
was created from the UI thread, then the RunWorkerCompleted
event will also be raised on the UI thread.
如果它是从后台线程创建的,该事件将提高在一个不确定的后台线程(不一定是同一个线程,除非你使用一个自定义的的SynchronizationContext
)。
If it was created from a background thread, the event will be raised on an undefined background thread (not necessarily the same thread, unless you're using a custom SynchronizationContext
).
有趣的是,这似乎并没有被所有的详细记录在MSDN上。最好的参考我能找到的是这里:
Interestingly, this doesn't seem to be all that well-documented on MSDN. The best reference I was able to find was here:
在你的应用程序中实现多线程的preferred的方法是使用BackgroundWorker零件。该BackgroundWorker组件使用多线程的事件驱动模型。 的后台线程运行的DoWork事件处理程序,创建你的控制线程运行的ProgressChanged和RunWorkerCompleted事件处理程序。您可以从您的ProgressChanged和RunWorkerCompleted事件处理程序。
这篇关于BackgroundWorker的RunWorkerCompleted事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!