Consider the following simple class:
Public MyClass
Private t As Thread
Shared LockObj As Object = New Object()
Public Sub New()
t = New Thread(AddressO f DoWork)
t.Start
End Sub
Private Sub DoWork()
SyncLock MyClass.LockObj
'Code to run in thread here
End SyncLock
End Sub
End Class
The code in DoWork will be run on the thread. Suppose that in that
method, I wish to write some data to a log file. Suppose, also, that
there will be several of these running and they each need to write to
the same file. Is it okay to use a Shared member as I have here for
synchronizing the thread? Is there a more preferred way?
Thanks
Public MyClass
Private t As Thread
Shared LockObj As Object = New Object()
Public Sub New()
t = New Thread(AddressO f DoWork)
t.Start
End Sub
Private Sub DoWork()
SyncLock MyClass.LockObj
'Code to run in thread here
End SyncLock
End Sub
End Class
The code in DoWork will be run on the thread. Suppose that in that
method, I wish to write some data to a log file. Suppose, also, that
there will be several of these running and they each need to write to
the same file. Is it okay to use a Shared member as I have here for
synchronizing the thread? Is there a more preferred way?
Thanks