create backgroundworker dynamically

peterpanes

New member
Joined
Sep 21, 2006
Messages
2
Programming Experience
Beginner
Hello, Please Help.

I’m writing a program that needs to have several threads running simultaneously, the number of threads that will be running will be stored in a database so I need to be able to create new threads dynamically at runtime. I need to pass a different IP address to each thread/backgroundworker each time i use it. Each thread will do the same thing but with a different IP address.

The problem is that i cant seem to create and run new instances of the background worker dynamically, I can put the background worker into a form and then create new instances of the form dynamically, but then I have to hide the forms and stuff and I want to avoid this and do it properly if possible. Does anyone know how to do this, I’m assuming it is possible to create a new instances of the backgroundworker at run time, or something, I’m not sure.

I have not used VB for 5 years so I cant remember much and all this dot net stuff is completely new to me.

Any help is much appreciated, Thanks
 
I suggest that you drop a BGW on a form then go have a look at the designer code that makes it work. You should then be able to replicate this behaviour

To see the designer code, click the "Show all Items" icon above the solution explorer, and Form1.Designer.vb will become visible
 
nameing a component dynamically

Thank for your postI am aware of how to initialize a background worker component but it seems as though i have to give it a name at design time.To over come this problem i really need to be able to create them (ok not a problem) but i need to be able to name them at runtime for example count = 0 do until count = 4 bgworker[count] = new backgroundworker count += 1 loopAssuming the above worked then i would have 6 threads started called:bgworker1, bgworker2, bgworker2 etci need to be able to give the backgroundworker a name dynamiclly at runtime or give a backgroundworker a name from the contents of a string or something.does anyone understand what im trying to do and have an answer.
 
VB.NET:
Dim bgwrks(6) As System.ComponentModel.BackgroundWorker
 
Private Sub startWorkers()
  For Each bgworker As System.ComponentModel.BackgroundWorker In bgwrks
    bgworker = New System.ComponentModel.BackgroundWorker
    AddHandler bgworker.DoWork, AddressOf myDoWork
[SIZE=2]    bgworker.RunWorkerAsync()
[/SIZE]  Next
End Sub
 
Private Sub myDoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
  'no work implemented
End Sub
 
Back
Top