从backgroundworker c调用backgroundworker# [英] call backgroundworker from backgroundworker c#

查看:115
本文介绍了从backgroundworker c调用backgroundworker#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#中调用backgroundworker中的backgroundworker?还想知道如何在c#中处理多个线程?

How to call backgroundworker from backgroundworker in c#? Also want to know about how to handle multiple thread in c#?

推荐答案

这里没什么特别的。您从另一个BackgroundWorker调用BackgroundWorker的方式与从主线程调用它的方式完全相同:通过 RunWorkerAsync() [ ^ ]方法。



或者使用 RunWorkerAsync(object) [ ^ ]重载。



然后BackgroundWorker激活其 DoWork [ ^ ]事件。订阅一个方法,在其中进行实际工作。该方法必须符合 DoWorkEventHandler [ ^ ],表示其返回类型为无效 [ ^ ]它需要两个参数:



第一个是对象类型的发件人 [ ^ ],实际上是对调用BackgroundWorker本身的引用。



其次是做WorkEventArgs [ ^ ],其中包含参数 [ ^ ],同样是object类型。这是对你给RunWorkerAsync(对象)重载的完全相同的参数的引用。
Nothing special here. You call a BackgroundWorker from another BackgroundWorker the exact same way you would call it from the main thread: via the RunWorkerAsync()[^] method.

Or use the RunWorkerAsync(object)[^] overload.

The BackgroundWorker then fires its DoWork[^] event. Subscribe a method to it in which you do the actual work. The method has to conform to a DoWorkEventHandler[^], which means its return type is void[^] and it needs two parameters:

The first is sender of type object[^], which actually is a reference to the calling BackgroundWorker itself.

Second is a DoWorkEventArgs[^], which encapsulates Argument[^], again of type object. That's a reference to the exact same argument you gave the RunWorkerAsync(object) overload.


BackgroundWorker线程只是另一个线程:所以你可以在普通线程中做任何事情你可以做在BackgroundWorker中(当然,访问UI组件除外) - 包括启动新线程。

您可能发现的唯一复杂性是跟踪哪个线程代码所在:但这很容易处理,因为BackgroundWorker实例作为 sender 参数传递给DoWork事件处理程序,只需要转换为BackgroundWorker。



请注意,并非所有数据项都是线程安全的:如果您正在增加线程,则可能需要开始使用锁和信号量来防止数据损坏。
A BackgroundWorker thread is just another thread: so anything you can do in a "normal" thread you can do in a BackgroundWorker (except access UI components, of course) - and that includes starting new threads.
The only complexity you might find is keeping track of which thread code is in: but that is pretty easy to handle since the BackgroundWorker instance is passed through to the DoWork event handler as the sender parameter, and just needs casting to a BackgroundWorker.

Be aware though that not all data items are thread safe: if you are multiplying threads you may need to start using locks and semaphores to prevent data corruption.


BackgroundWorker类可以使线程易于使用,因为它将ProgressChanged和RunWorkerCompleted事件编组到UI线程。但是等待赶上你的是缺乏文件证明要求必须从UI线程调用 RunWorkerAsync方法



如果调用RunWorkerAsync从非UI线程,然后事件将不会被编组并将在ThreadPool线程上引发。在这种情况下,直接访问Control将导致交叉线程异常,如果ProgressChanged事件以高速率提升,则可能无法到达。



所以是的,你可以从另一个开始一个BackgroundWorker,但可能希望你没有。



Alan。
The BackgroundWorker class can make threading simple to use because it marshals the ProgressChanged and RunWorkerCompleted events to the UI thread. BUT waiting to catch you out is the poorly documented requirement that the RunWorkerAsync method must be called from the UI thread.

If RunWorkerAsync is called from a non UI thread then the events will not be marshalled and will be raised on ThreadPool threads. In this situation accessing a Control directly will cause a cross threading exception and if ProgressChanged events are raised at a high rate they may arrive out of order.

So yes, you can start one BackgroundWorker from another but will probably wish that you hadn't.

Alan.


这篇关于从backgroundworker c调用backgroundworker#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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