473,395 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Threading a routine with parameters?

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
'
'\\\\\\\\\\\\\\\
Nov 21 '05 #1
3 1996
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

"Amjad" wrote:
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
'
'\\\\\\\\\\\\\\\

Nov 21 '05 #2
"Amjad" <Am***@discussions.microsoft.com> schrieb:
How do I start a thread of a sub routine that takes parameters?


<URL:http://www.google.de/groups?selm=%23mGf7SV%23CHA.2044%40TK2MSFTNGP10.ph x.gbl>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
Excellent SIMPLE example but yet expandable to using many parameters that
works great! Thanks for posting.
--
Dennis in Houston
"Brian Haynes" wrote:
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

"Amjad" wrote:
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
'
'\\\\\\\\\\\\\\\

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
15
by: DanielEKFA | last post by:
Hey there :) Just joined the group, looking for a resolution to a problem I and my three groups members are having (we're students, making an FTP-like server/client as an exam project). We're...
2
by: AtherMurtuzapurwala | last post by:
Hi All, Client/Server Program using Sockets... Server sends request in form of CSV format in NetworkStream... So I want to read this using threading because it is in very large volume so...
2
by: linesh.gajera | last post by:
Hi Guys, I am creating a Windows service that call a routine at given interval. Once routine is complete, windows service should wait for 5 minutes and then call the routine again. I was using...
3
by: KC | last post by:
Hey, I'm trying to implement a cancel button in my app (this is after the bulk of the program has been built). To do this I, of course, need to use a thread to run the time consuming method...
16
by: One Handed Man \( OHM - Terry Burns \) | last post by:
Sorry if this gets duplicated, but I posted it and cant see it for a long time so repost. . . I have an application which is writing to a graphics object, the main UI thread and a worker thread...
4
by: Phil G. | last post by:
I was recently struggling to adapt an example I have using delegate methods, IasynResult and AsynCallback. Doing a little research I came across an example, which in fact was being used to...
14
by: Simon Verona | last post by:
I think I'm doing this wrong : I have a class with a public shared method such as follows: public shared sub myFunction dim frm as new myFrm dim t as new Threading.Thread(Addressof ...
4
by: | last post by:
I am working with an application that requires multithreading. I have found a sample online that I am currently working with/learning from. However, I am not sure if this is the most effecient...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.