Smith
Smith

Reputation: 5959

multiple backgroundworker vb.net

I need to run multiple task in the background using backgroundwoker

e.g dim mybacgroundwokers(10) as backgroundwoker

in the backgroundworker event procedure, can i do somthing like this, cos i don't have an idea

mybackgroundwoker_dowork(,,index)

select case index
case 1
'dowork for backgroundworker1
case 2
'dowork for backgroundworker2
end select

or how do i handle multiple backgroundworker

if non of this is possible with the backgroundworker, how do i do this using thread multithreading?

thanks

Upvotes: 2

Views: 6697

Answers (2)

Stephan B
Stephan B

Reputation: 3701

You should use multiple Backgroundworkers with a suitable name, like:

Dim CountSheepBackgroundWorker As BackgroundWorker WithEvents
Dim CountStarsBackgroundWorker As BackgroundWorker WithEvents
Dim StackEggsBackgroundWorker As BackgroundWorker WithEvents

If you absolutely want to use an Array, Visual Studio won't help you with the DoWork Subs:

Dim MyBackgroundWorkers(9) As BackgroundWorker
' Add first
MyBackgroundWorkers(0) = new BackgroundWorker
AddHandler MyBackgroundWorkers(0), AddressOf FirstBackgroundWorker_DoWork
MyBackgroundWorkers(0).RunWorkerAsync()
' Add second etc

Upvotes: 2

DarinH
DarinH

Reputation: 4889

You might want to look at this thread

Trigger Backgroundworker Completed event

Your DIM command is simply creating an array that CAN store up to 11 (remember, indexes start at 0) background worker objects.

So you'd then need to create 11 new BackgroundWorker objects and store them in each element of the array.

Then, you'd need to configure each BackgroundWorker object on it's own to do whatever work was necessary. Putting them in an array doesn't change any of that.

Upvotes: 2

Related Questions