2

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

Smith
  • 5,765
  • 17
  • 102
  • 161

2 Answers2

2

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.

Community
  • 1
  • 1
DarinH
  • 4,868
  • 2
  • 22
  • 32
  • that does not relate to my question. am trying to simple creat multiple thread to handle more task in my application, and i resort to using the backgroundworker. AM new in the subject, do i need advise and help – Smith Mar 20 '11 at 09:09
  • Ok, but you need to remember that as difficult as programming can be, multithreaded programming can be a couple of +orders of magnitude+ more difficult. It's virtually +never+ the case that you can do anything "simply" with multithreading. Not trying to disuade you from it, I'm just saying that you +really+ need to have a solid understanding of why you're wanting to add multiple threads and what you can accomplish using that technique before diving in. – DarinH Mar 22 '11 at 14:31
  • For instance, are you looking to allow background functions while keeping a UI "active" and preventing the app from looking like its locked up? Or do you simply need to process a bunch of queues simultaneously? If the latter, it might make more sense to create a seperate App running in a seperate process that you can just spin up whole new processes as necessary, and use DB tables to record progress of each worker. There's lots of ways to approach something like that, and threading is a complex topic. – DarinH Mar 22 '11 at 14:33
  • @devwnture take for example, i need to download say 20 textfile, process each and upload back to my server, i could need one to download all the files one by one, the other worker should look for the file and process it, and the last worker should look for processed files and upload it back. this is a sample task where i need at leat three background workers – Smith Mar 24 '11 at 17:34
2

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
Stephan B
  • 3,671
  • 20
  • 33
  • Select the first Backgroundworker in the Designer and add a DoWork Handler by doubleclicking the Event, in the other Backgroundworkers just select the existing Sub from the Dropdown. This extends the `Handles ...` Clause of the Sub. Alternatively you can use multiple `AddHandler BackgroundWorker1 ...` with the same Sub. – Stephan B Jun 01 '11 at 10:34