By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,688 Members | 1,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,688 IT Pros & Developers. It's quick & easy.

Passing data to a thread

P: n/a
I have seen the following example of passing data to a thread:

Class ThreadData
Public Id As Integer
Public Msg As String
Public Done As Boolean
'
The entry point for the thread

Sub DoTheTask()
For i As Integer = 1 To 10
' Show that the thread received the correct values.
Console.WriteLine("{0} (Thread ID = {1})", msg, id)
' Wait for 0.2 second.
Thread.Sleep(200)
Next
' Let the main thread know that this thread has completed.
Done = True
End Sub
End Class
I am a little puzzled about the use of a public variable here and the
possibilities of it being overwritten by another thread. It raised a
question in my mind about the scope of public variables. Are they public
throughout the whole project or just within a class?

-Jerry
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies

P: n/a
Hi,

They are public to the class. If you have several instances of
the class each will have there own public variables. In case you were
wondering the synclock statement will help prevent a variable from being
overwritten.

http://msdn.microsoft.com/library/de...tmsynclock.asp

Ken
-------------------
"Jerry Spence1" <je**********@somewhere.com> wrote in message
news:42**********************@ptn-nntp-reader03.plus.net...
I have seen the following example of passing data to a thread:

Class ThreadData
Public Id As Integer
Public Msg As String
Public Done As Boolean
'
The entry point for the thread

Sub DoTheTask()
For i As Integer = 1 To 10
' Show that the thread received the correct values.
Console.WriteLine("{0} (Thread ID = {1})", msg, id)
' Wait for 0.2 second.
Thread.Sleep(200)
Next
' Let the main thread know that this thread has completed.
Done = True
End Sub
End Class
I am a little puzzled about the use of a public variable here and the
possibilities of it being overwritten by another thread. It raised a
question in my mind about the scope of public variables. Are they public
throughout the whole project or just within a class?

-Jerry

Nov 21 '05 #2

P: n/a
Thanks very much Ken. That clears it up for me

-Jerry
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,

They are public to the class. If you have several instances of
the class each will have there own public variables. In case you were
wondering the synclock statement will help prevent a variable from being
overwritten.

http://msdn.microsoft.com/library/de...tmsynclock.asp

Ken
-------------------
"Jerry Spence1" <je**********@somewhere.com> wrote in message
news:42**********************@ptn-nntp-reader03.plus.net...
I have seen the following example of passing data to a thread:

Class ThreadData
Public Id As Integer
Public Msg As String
Public Done As Boolean
'
The entry point for the thread

Sub DoTheTask()
For i As Integer = 1 To 10
' Show that the thread received the correct values.
Console.WriteLine("{0} (Thread ID = {1})", msg, id)
' Wait for 0.2 second.
Thread.Sleep(200)
Next
' Let the main thread know that this thread has completed.
Done = True
End Sub
End Class
I am a little puzzled about the use of a public variable here and the
possibilities of it being overwritten by another thread. It raised a
question in my mind about the scope of public variables. Are they public
throughout the whole project or just within a class?

-Jerry

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.