Threading a routine with parameters?

G

Guest

Hi,

To start a new thread of a sub routine I use:
'///////////
Dim t as Thread = New Thread (AddressOf MySub)
t.Start
'\\\\\\\\\\\

How do I start a thread of a sub routine that takes parameters?
For example:
'////////////
Sub MySub (filePath as String)
if File.Exists(filePath) Then File.Delete(filePath)
End Sub
'
'
Dim t as Thread = New Thread (AddressOf MySub("C:\test.txt"))
t.Start
'
'\\\\\\\\\\\\\\\
 
G

Guest

I don't know if it is possible to do it with a non-member function. What I
usually do in this situation would be to declare a class that takes the
arguments I need in its constructor and then make the method I want to call
have no parameters so I can use it on the thread.

For Example:

Public Class ThreadWorker
Private path as String

Public Sub New(filePath as String)
path = filePath
End Sub

Public Sub MySub()
If File.Exists(path) Then File.Delete(path)
End Sub
End Class

....

Dim worker as new ThreadWorker("C:\test.txt")
Dim t as new Thread(AddressOf worker.MySub)
t.Start()

Hope this helps,
Brian
 
G

Guest

Excellent SIMPLE example but yet expandable to using many parameters that
works great! Thanks for posting.
 

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

Similar Threads

Threading Question 2
Class wth Thread 9
Threading with forms 5
Thead Completion 3
Problem with thread 5
Some Thread questions 3
Threading won't work on a function with parameters 2
basic threading question 2

Top