使用单个DoWork子运行多个BackgroundWorkers [英] run multiple BackgroundWorkers with single DoWork sub

查看:107
本文介绍了使用单个DoWork子运行多个BackgroundWorkers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

如何使用单个DoWork和progresschanged子运行多个BackgroundWorkers?
例如,
之类的东西

Hello,

how do i run multiple BackgroundWorkers with single DoWork and progresschanged sub?
for example, somthing like

dim ListOfTask as new list(Of string)

TestWorker1 = New System.ComponentModel.BackgroundWorker
TestWorker2 = New System.ComponentModel.BackgroundWorker
AddHandler TestWorker1.DoWork, AddressOf TestWorker_DoWork
AddHandler TestWorker2.DoWork, AddressOf TestWorker_DoWork

private sub TestWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
if listoftask.count >1 then
''// do the work with a new worker
end if
end sub









will this work?

推荐答案

在两个单独的线程中运行相同的方法与在两个不同的线程中运行两个不同的方法没有什么不同.如果考虑线程如何工作,您将清楚地看到它.

这完全取决于此方法的编写方式.线程同步的预防措施也相同,例如对任何共享资源的访问进行同步.可能会发生同样的线程危险,例如死锁( http://en.wikipedia.org/wiki /Deadlock [ ^ ])或种族条件( http://en.wikipedia.org/wiki/Race_condition [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

注意线程包装技术,非常重要且方便:
如何将ref参数传递给线程 [ ^ ].
VSNetVbHarry转换为VB.NET:
将参数传递给线程化的LongRunningProcess [
Running the same method in two separate thread is no different from running two different methods in two different threads. You will clearly see it if you consider how threads work.

It all depends on how this methods is written. The same precautions to thread synchronization apply, such as synchronization of access to any shared resources. The same very threading hazards may occur, such as deadlocks (http://en.wikipedia.org/wiki/Deadlock[^]) or race conditions (http://en.wikipedia.org/wiki/Race_condition[^]).


This is a collection of my past answer on the topic:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

Pay attention for the thread wrapper technique, very important and convenient:
How to pass ref parameter to the thread[^].
Translated to VB.NET by VSNetVbHarry:
Passing arguments to a threaded LongRunningProcess[^].

—SA


这篇关于使用单个DoWork子运行多个BackgroundWorkers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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