Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a variable to a sub for a new thread

Status
Not open for further replies.

Tbird761

Programmer
Dec 23, 2004
3
US
Hello folks. I've got an odd situation here. I need to pass a variable to a sub that I want to make into a new thread. My current conundrum is as follows:

Dim Mults(127) as Threading.Thread
Dim n As Integer
For n = 0 to 127
Mults(n) = New Threading.Thread(AddressOf SomeSub(n))
Next

If I do it that way, it says that I can't have the (n). If I do this this way:

Dim Mults(127) as Threading.Thread
Dim n As Integer
For n = 0 to 127
Mults(n) = New Threading.Thread(AddressOf SomeSub)
Next

It of course doesn't match my sub's params. I need to pass a variable to the sub in order to be useful. It wouldn't be such a big deal, but the sub can take up to 2 seconds waiting for a response, so I want to make each a new thread and just wait a single 2 seconds instead of 127 * 2 seconds.

How can I pass the variable to the sub/function of the new thread?
 
Simple answer you can not pass argument when using AddressOf, here is another link that explains the same.

thread796-962915

-Kris
 
This might be a problem, as I need to work with already made objects within an existing class with the new threads. Maybe there is a way I can just execute code in an async method?
 
I've used a little cheat to get arround this issue.

create a class. Give that class properties for all of the parameters you need to use. Give it a public sub that has the code you want to execute in a thread. then in your loop dim a new object of that class time, asign the parameters to the properties, then set the address of to the sub in your new object. That way you don't have to deal with deligates, synching or any other hassles. Kind of a "Shoot from the hip" approach to multi-threading.

-Rick



----------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top