B
Bob Day
VS 2003, vb.net, sql msde...
The help is pretty empatic that you cannot pass parameters to a thread. The
sample below is from help, showing you to set up variables in the
TasksClass, and assign their value from sub DoWork where you start the
thread. This approach works, of course.
Why can't you simply add a Sub New to the TaskClass below, and then in sub
DoWork modify the threading line with the sub new arguments needed like
this: Dim Thread1 As New System.Threading.Thread(addressOf
Tasks.SomeTask(subnewarugment1, subnewargument2, etc.) This seems to work
also, and is the way I have been doing it. Is there any down side to this
method?
Thanks!
Bob Day
Class TasksClass
Friend StrArg As String
Friend RetVal As Boolean
Sub SomeTask()
' Use the StrArg field as an argument.
MsgBox("The StrArg contains the string " & StrArg)
RetVal = True ' Set a return value in the return argument.
End Sub
End Class
' To use the class, set the properties or fields that store parameters,
' and then asynchronously call the methods as needed.
Sub DoWork()
Dim Tasks As New TasksClass
Dim Thread1 As New System.Threading.Thread(addressOf Tasks.SomeTask)
Tasks.StrArg = "Some Arg" ' Set a field that is used as an argument
Thread1.Start() ' Start the new thread.
Thread1.Join() ' Wait for thread 1 to finish.
' Display the return value.
MsgBox("Thread 1 returned the value " & Tasks.RetVal)
End Sub
The help is pretty empatic that you cannot pass parameters to a thread. The
sample below is from help, showing you to set up variables in the
TasksClass, and assign their value from sub DoWork where you start the
thread. This approach works, of course.
Why can't you simply add a Sub New to the TaskClass below, and then in sub
DoWork modify the threading line with the sub new arguments needed like
this: Dim Thread1 As New System.Threading.Thread(addressOf
Tasks.SomeTask(subnewarugment1, subnewargument2, etc.) This seems to work
also, and is the way I have been doing it. Is there any down side to this
method?
Thanks!
Bob Day
Class TasksClass
Friend StrArg As String
Friend RetVal As Boolean
Sub SomeTask()
' Use the StrArg field as an argument.
MsgBox("The StrArg contains the string " & StrArg)
RetVal = True ' Set a return value in the return argument.
End Sub
End Class
' To use the class, set the properties or fields that store parameters,
' and then asynchronously call the methods as needed.
Sub DoWork()
Dim Tasks As New TasksClass
Dim Thread1 As New System.Threading.Thread(addressOf Tasks.SomeTask)
Tasks.StrArg = "Some Arg" ' Set a field that is used as an argument
Thread1.Start() ' Start the new thread.
Thread1.Join() ' Wait for thread 1 to finish.
' Display the return value.
MsgBox("Thread 1 returned the value " & Tasks.RetVal)
End Sub