The SyncLock..End SyncLock Statement

The Visual Basic language offers the SyncLock..End SyncLock statement, which is the place where you can grant access to the specified resource to only one thread per time. For example, imagine you have a class where you define a list of customers and a method for adding a new customer to the list, as demonstrated by the following code snippet:

Private customers As New List(Of String)Sub AddCustomer(customerName As String)    SyncLock Me        customers.Add(customerName)    End SyncLockEnd Sub

The preceding code locks the entire enclosing class, preventing other threads from accessing the instance until the requested operation completes. Locking an entire class is not always the ...

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.