Passing an argument through threads

  • Thread starter Thread starter Overburn
  • Start date Start date
O

Overburn

I have a long procedure that i would like to have run on its own thread, but
the procedure requires a variable that I would pass to it.. I would like to
avoid using a global variable. Can someone show me how to pass a variable
through a threaded function.
 
I was reading the VS Help file on the "setData" method. Couldn't that be
used in this instance, It seems like it is saying that I can set a certain
slot set to a specified value for a specified type. Only reason I ask is
because my program isn't ready for a test run or else I would try it out...
 
* "Overburn said:
I have a long procedure that i would like to have run on its own thread, but
the procedure requires a variable that I would pass to it.. I would like to
avoid using a global variable. Can someone show me how to pass a variable
through a threaded function.

Sample taken from MSDN (slightly modified):

\\\
Imports System
Imports System.Threading

Public Class SimpleThread
Delegate Sub Start(o As Object)

Private Class Args

Public o As Object
Public s As Start

Public Sub work()
s(o)
End Sub
End Class

Public Shared Function CreateThread(s As Start, arg As Object) As
Thread
Dim a As New Args()
a.o = arg
a.s = s ' Arg enthält die Argumente für den Thread.
Dim t As New Thread(AddressOf a.work)
Return t
End Function
End Class

Class Worker
Public Shared Sub WorkerMethod(o As Object)
Console.WriteLine("WorkerMethod: " & o.ToString())
End Sub 'WorkerMethod
End Class 'Worker

Public Class Work
Public Shared Sub Main()
Dim t As Thread = _
SimpleThread.CreateThread(AddressOf Worker.WorkerMethod, 51)
t.Start()
t.Join(Timeout.Infinite)
End Sub
End Class
///
 
Back
Top