The SyncLock..End SyncLock Statement

The Visual Basic language offers the SyncLock..End SyncLock statement that 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:

image

Basically the preceding code locks the entire enclosing class, preventing other threads from accessing the instance until the requested operation completes. By the way, locking an entire class is not always the best idea, because it can be expensive in terms of resources ...

Get Visual Basic® 2010 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.