BackgroundWorker RunWorkerCompleted 事件 [英] BackgroundWorker RunWorkerCompleted Event

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

问题描述

我的 C# 应用程序有几个后台工作人员.有时一个后台工作人员会解雇另一个.当第一个后台工作线程完成并触发 RunWorkerCompleted 事件时,该事件将在哪个线程上触发,UI 或调用 RunWorkerAsync 的第一个后台工作线程?我使用的是 Microsoft Visual C# 2008 速成版.您的任何想法或建议将不胜感激.谢谢.

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:

在应用程序中实现多线程的首选方法是使用 BackgroundWorker 组件.BackgroundWorker 组件使用事件驱动模型进行多线程.后台线程运行您的 DoWork事件处理程序,并且创建您的控件的线程运行您的 ProgressChangedRunWorkerCompleted 事件处理程序. 您可以从 ProgressChanged<调用您的控件/a> 和 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天全站免登陆