|
-
Jun 12th, 2011, 11:37 AM
#1
Thread Starter
Frenzied Member
Send Varible to Sub on new thread
Hi again guys,
I have created a class with a public sub which has a varible with it using byval:
Public Class clsMessenger
Public Sub Messenger(ByVal Port As Integer)
End Sub
End Class
I now need to start that sub in a new thread and pass a varible to it but I am not entirly sure how to go about that.
At the moment I have:
Dim ClientConnection As New clsMessenger
Dim ClientThread As New Threading.Thread(AddressOf ClientConnection.Messenger)
ClientThread.Start()
I can't use a global varible because there is going to be multiple instances of this routine running on different threads and each time the varible to be passed will be different.
Any advice?
Thanks.
-
Jun 12th, 2011, 11:43 AM
#2
Re: Send Varible to Sub on new thread
you can pass a single parameter of type object like this:
vb Code:
Public Class clsMessenger
Public Sub Messenger(ByVal Port As Object)
End Sub
End Class
vb Code:
Dim ClientConnection As New clsMessenger
Dim ClientThread As New Threading.Thread(AddressOf ClientConnection.Messenger)
ClientThread.Start(variable)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 12th, 2011, 11:52 AM
#3
Re: Send Varible to Sub on new thread
You might like to follow the CodeBank link in my signature and check out my thread on Passing Data To Threads. It provides several variations.
-
Jun 12th, 2011, 11:52 AM
#4
Fanatic Member
Re: Send Varible to Sub on new thread
What I do is different than you have outlined.
I create a class (object) that contains a threaded process. When I instantiate an instance of that object, I start the thread via a specific call (perhaps named Start) - and start the thread using that method. Thus, I can pass in all required parameters in using the object constructor, fields, or actual full-blown properties.
A simplification would be to simply use the object constructor (New), and start the thread when the object is created, and pass in the parameter as an argument to New. However, I prefer the process that I outlined above, because it is more flexible, allowing me to do parameter validation in a more straight forward way.
Either way. The thread then runs using instance variables that are specific to that object.
Dick
Richard Grier, Consultant, Hard & Software
Microsoft MVP (Visual Basic)
-
Jun 12th, 2011, 12:31 PM
#5
Re: Send Varible to Sub on new thread
 Originally Posted by DickGrier
I create a class (object) that contains a threaded process. When I instantiate an instance of that object, I start the thread via a specific call (perhaps named Start) - and start the thread using that method. Thus, I can pass in all required parameters in using the object constructor, fields, or actual full-blown properties.
I know what you mean but I've got a problem with some of that terminology. First up, a class doesn't contain a threaded process. If you're doing what I think you're doing, which is demonstrated in my CodeBank thread, your class doesn't actually know about the multi-threading and its the consumer of the class that starts the new thread. Your class simply contains a method.
Secondly, you don't instantiate an instance of an object. You instantiate a type, which means creating an instance of that type. The instance is the object.
Terminology aside, that method is generally outdated. People tended to use it because there was originally no way to directly pass data to a thread entry method. As demonstrated in my CodeBank thread, there are now two options for doing just that now, so creating a dedicated class is no longer necessary and, assuming VB 2010, offers no advantage.
-
Jun 13th, 2011, 10:49 AM
#6
Fanatic Member
Re: Send Varible to Sub on new thread
Jim,
I think you misunderstand. A class (type) can contain a threaded process. It doesn't have to, but I didn't imply any such thing.
As to "outmoded," perhaps. As to "no advantage," I less certain. I have a lot of code that uses this "outmoded" pattern, and it works well, thanks. And, if you have lots of threads running via various instances, it allows fairly simple interaction with the threads.
However, your comment that you don't instantiate an instance of an object, is correct. I meant, instantiate an instance of the class (call this a type, if you prefer). Your comment is a quibble, that is, pedantry. I've done similar, so (perhaps) I souldn't complain.
Dick
Richard Grier, Consultant, Hard & Software
Microsoft MVP (Visual Basic)
-
Jun 13th, 2011, 11:29 AM
#7
Re: Send Varible to Sub on new thread
People say that like it's a bad thing... I take it as a compliment... and for the sake of my future, I HOPE that the developer of my pace maker is pedantic and not some lazy "close enough", "but I really meant" developer.
-tg
-
Jun 13th, 2011, 12:10 PM
#8
Re: Send Varible to Sub on new thread
 Originally Posted by techgnome
developer of my pace maker
Is this your current developer or future one?
-
Jun 13th, 2011, 12:27 PM
#9
Re: Send Varible to Sub on new thread
the future developer of my future pacemaker....I ain't ready for one just yet sonny... I haven't reached the point where I'm yelling gitoffamahlawn at the neighborhood hoodlums yet...
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|