Thread procedure with arguments?
-
Hi, I know a little about multi-threading in vb.net I know that we can execute a procedure on a new thread by using:
Dim t as New Thread(AddressOf proc)
"proc" should be of the form :Sub proc() 'code... End Sub
Can i make proc a sub with arguments? like this:Sub proc(str as String) 'code... End Sub
and call proc on a new thread and specify str? I mean something like:Dim t as New Thread(AddressOf proc("hello"))
If yes, how? -
Hi, I know a little about multi-threading in vb.net I know that we can execute a procedure on a new thread by using:
Dim t as New Thread(AddressOf proc)
"proc" should be of the form :Sub proc() 'code... End Sub
Can i make proc a sub with arguments? like this:Sub proc(str as String) 'code... End Sub
and call proc on a new thread and specify str? I mean something like:Dim t as New Thread(AddressOf proc("hello"))
If yes, how? -
Hi, I know a little about multi-threading in vb.net I know that we can execute a procedure on a new thread by using:
Dim t as New Thread(AddressOf proc)
"proc" should be of the form :Sub proc() 'code... End Sub
Can i make proc a sub with arguments? like this:Sub proc(str as String) 'code... End Sub
and call proc on a new thread and specify str? I mean something like:Dim t as New Thread(AddressOf proc("hello"))
If yes, how?ThreadStart's can't have arguments. Normally, you would put this code into a class, along with some public fields that would represent the parameters for this method you want to start. Then you create and instance of the class, set the fields to the values you need, create your ThreadStart object and point it at the method in your instantiated class, then run it. The method would have to pick up its parameters from the public fields you set in its class.
Public Class MyThreadClass
Public argument1 As Integer
Public Sub MyMethod()
Dim whatever As Integer = argument1
.
. your method code...
.
End Sub
End Class
' Code in your calling class.
Dim mtc As New MyThreadedClass
mtc.argument1 = 30
Dim t As New Thread( AddressOf mtc.MyMethod )RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Hi, I know a little about multi-threading in vb.net I know that we can execute a procedure on a new thread by using:
Dim t as New Thread(AddressOf proc)
"proc" should be of the form :Sub proc() 'code... End Sub
Can i make proc a sub with arguments? like this:Sub proc(str as String) 'code... End Sub
and call proc on a new thread and specify str? I mean something like:Dim t as New Thread(AddressOf proc("hello"))
If yes, how?You could use a delegate then invoke it asynchronously. Delegates can take arguments unlike threads.
-
You could use a delegate then invoke it asynchronously. Delegates can take arguments unlike threads.
Can you please give me a short example? What is a delegate? I would apreciate your help. Thanks in advance