Click here to Skip to main content
15,944,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Updated 31-May-11 7:36am
v3

1 solution

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[^]).

[EDIT — per our discussion]
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
 
Share this answer
 
v3
Comments
Cool Smith 31-May-11 12:43pm    
each worker will have to work with a different variable
Sergey Alexandrovich Kryukov 31-May-11 19:10pm    
No! Who tells you so? Not true. What variable?
They can work with shared data, separate data, whatever required. It's only important to do proper synchronization.
--SA
Sergey Alexandrovich Kryukov 6-Jun-11 12:07pm    
At the same time I think I understand what do you mean. Each thread works with its own version of data. Right. That's why I reference a thread wrapper. The individual storage for data will be per instance of the wrapper. You have one instance of the wrapper per instance of the thread, so you will not share individual thread's data.
--SA
Cool Smith 2-Jun-11 13:00pm    
can you help me with a sample code that uses background workers?
Sergey Alexandrovich Kryukov 2-Jun-11 13:36pm    
What kind? How about original Microsoft sample on MSDN page:
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

Why background worker? In many cases an explicitly created thread is better. Please explain your ultimate goals, if you have those.
I have a lot of matter on threading here at CodeProject, will give you a collection in links to my past answer. You will learn what's involved and then ask you questions.
If you pose a new question (create a separate page), that would be fine, but don't forget to notify me by replying to this comment, use "Reply" on top right.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900