ThreadStatic field : ThreadStatic « Thread « VB.Net Tutorial






Imports System.Threading

Public Class SharedByThread
   <ThreadStatic> Private Shared Count As Integer

   Public Shared Sub Main()
   
      Dim secondThread As New thread(AddressOf Thread2Proc)
      secondThread.Start()

      count = 100

      For ctr As Integer = 0 to 10
         Console.WriteLine("The value of count in the main thread is {0}.", count)
         IncrementCounter(200)
      Next
   End Sub

   Public Shared Sub Thread2Proc()
      count = 0

      For ctr As Integer = 0 to 10
         Console.WriteLine("The value of count in the second thread is {0}.", count)
         IncrementCounter(250)
      Next
   End Sub

   Public Shared Sub IncrementCounter(delay As Integer)
      count +=1
      Thread.Sleep(delay)
   End Sub
End Class
The value of count in the second thread is 0.
The value of count in the main thread is 100.
The value of count in the main thread is 101.
The value of count in the second thread is 1.
The value of count in the main thread is 102.
The value of count in the second thread is 2.
The value of count in the main thread is 103.
The value of count in the second thread is 3.
The value of count in the main thread is 104.
The value of count in the second thread is 4.
The value of count in the main thread is 105.
The value of count in the main thread is 106.
The value of count in the second thread is 5.
The value of count in the main thread is 107.
The value of count in the second thread is 6.
The value of count in the main thread is 108.
The value of count in the second thread is 7.
The value of count in the main thread is 109.
The value of count in the second thread is 8.
The value of count in the main thread is 110.
The value of count in the second thread is 9.
The value of count in the second thread is 10.








23.10.ThreadStatic
23.10.1.ThreadStatic field
23.10.2.ThreadStatic