Dim someThread As New Thread(AddressOf SomeMethod) someThread.Start()
Dim someClass As New SomeClass Dim someThread As New Thread(AddressOf someClass.SomeMethod) someThread.Start()
Dim someObject as New Object ThreadPool.QueueUserWorkItem(AddressOf SomeMethod, someObject)
Dim someThread as New Thread(AddressOf SomeMethod) someThread.Priority = ThreadPriority.Highest
Dim someThread as New Thread(AddressOf SomeMethod) someThread.IsBackground = True
Dim lockObject As New Object Sub SomeMethod() SyncLock lockObject 'critical section goes here End SyncLock End Sub
Imports System.Runtime.CompilerServices Class SomeClass <MethodImpl(MethodImplOptions.Synchronized)> _ Sub SomeMethod() 'critical code End Sub End Class
Imports System.Runtime.Remoting.Contexts <Synchronization()> _ Class SomeClass Inherits ContextBoundObject 'everything is critical! End Class
Dim lockObject As New Object Monitor.Enter(lockObject) === SyncLock lockObject Monitor.Exit(lockObject) === End SyncLock
Dim lockObject As New Object Sub SomeMethod() If Monitor.TryEnter(lockObject) Then Try 'you've obtained the lock and have entered the critical section Finally Monitor.Exit(lockObject) End Try Else 'No lock for you! End If End Sub
Dim lockCounter As Integer Sub SomeMethod() Try If Interlocked.Increment(lockCounter) 'critical section Else 'there are more than four threads in the critical section...no lock for you! End If Finally Interlocked.Decrement(lockCounter) End Try End Sub
Dim someMutex As New Mutex Dim lockObject As New Object someMutex.WaitOne() === Synclock lockObject someMutex.ReleaseMutex() === End Synclock someMutex.WaitOne(100, False) === Monitor.TryEnter(lockObject, 100)
Dim someMutexes() As Mutex = {New Mutex, New Mutex, New Mutex} Sub SomeMethod() Dim someInteger As Integer someInteger = Mutex.WaitAny(someMutexes) Try 'critical section Finally someMutexes(someInteger).ReleaseMutex() End Try End Sub
Dim someMutexes() As Mutex = {New Mutex, New Mutex, New Mutex} Sub SomeMethod() Mutex.WaitAll(someMutexes) 'all Mutex objects are now released End Sub
Dim someMutex As New Mutex("False", "some name")
printable version chaos
Everything2 Help