Passing parameter to a Threaded function?

  • Thread starter Thread starter Sam Learner
  • Start date Start date
S

Sam Learner

Hello everyone,
I am developping an application, I create a thread for the application
because it is about to download a large file, and wanted it to do it inside
of a thread... Now, the function I need the thread to call has a
parameter...
How do I call a function with the addressof(..) with a parameter value?

for example:
public function processData(byval data as arraylist) as boolean....
....
end function

dim thrd as new thread(addressof(processData))
thrd.start( )

I have not be able to find a way to pass a value to data...

Please help...
SJ
 
Try this. You have to delcate the function you wand called at the beginning
of the class as a delegate.

Then you have to create a class inside the main class which contains the
variables you need to send into the function. Also create a function with no
paramaters that will call the delegate

Then you create the actual function that the thread will call. it's
paramaters must match the Delegate.

Then create an instance of the class in the calling function. set the
variables and the address of the function being called in the class.



Private Delegate Sub DeleteDelegate(ByVal MessageAs String) 'Paramaters must
match the function being called



Private Class DeleteArgs

Public cMessage As String

Public StartDelegate As DeleteDelegate 'instance of function to call

Public Sub DeleteFiles()

StartDelegate(cMessage)

End Sub

End Class



Public Sub DeleteFiles(ByVal pMessage As String)

msgbox(pMessage)


End Sub



public sub FunctionToLaunchThreads()

Dim ca As DeleteArgs 'instance of class

Dim t As Thread



ca = New DeleteArgs

ca.cMessage = "Testing the threading function")

ca.StartDelegate = AddressOf DelFiles 'set the function to call

t = New Thread(AddressOf ca.DeleteFiles) 'set the thred to the function of
the class

t.Name = "Thread Delete Old Files"

t.ApartmentState = ApartmentState.MTA

t.Start()

end sub

Brad Shook
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top