J
Jerry Spence1
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
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