BackgroundWorker的RunWorkerCompleted事件 [英] BackgroundWorker RunWorkerCompleted Event

查看:686
本文介绍了BackgroundWorker的RunWorkerCompleted事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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的方法是使用<一个href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx">BackgroundWorker零件。该<一href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx">BackgroundWorker组件使用多线程的事件驱动模型。 的后台线程运行的<一个href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.dowork.aspx">DoWork事件处理程序,创建你的控制线程运行的<一个href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.progresschanged.aspx">ProgressChanged和<一href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.runworkercompleted.aspx">RunWorkerCompleted事件处理程序。您可以从您的<一个打电话给你的控件href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.progresschanged.aspx">ProgressChanged和<一href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.runworkercompleted.aspx">RunWorkerCompleted事件处理程序。

The preferred way to implement multithreading in your application is to use the BackgroundWorker component. The BackgroundWorker component uses an event-driven model for multithreading. The background thread runs your DoWork event handler, and the thread that creates your controls runs your ProgressChanged and RunWorkerCompleted event handlers. You can call your controls from your ProgressChanged and RunWorkerCompleted event handlers.

这篇关于BackgroundWorker的RunWorkerCompleted事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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