Synchronization with the Monitor Class
The System.Threading.Monitor
class is the support object for the SyncLock..End SyncLock
statement, and the compiler translates SyncLock
blocks into invocations to the Monitor
class. You use it as follows:
Sub AddCustomer(customerName As String) Dim result As Boolean Try Monitor.Enter(lockObject, result) customers.Add(customerName) Catch ex As Exception Finally If result Then Monitor.Exit(lockObject) End TryEnd Sub
Tip
Monitor.Enter
has an overload that takes a second argument of type Boolean
, passed by reference, indicating whether the lock was taken.
Monitor.Enter
locks the object; Monitor.Exit
unlocks it. It is fundamental to place
Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.