Controlling Access to Data Objects with the Monitor Class

The Monitor class provides another synchronization mechanism available in the .NET Compact Framework. The Monitor class is useful for protecting access to specific objects. You can use a Monitor to make sure that only one thread accesses a specific object at a time. This intent is subtly different from using a Mutex, which you can use to prevent two threads from executing a specific block of code at one time.

To use a Monitor to prevent multiple threads from accessing the same object, follow these steps:

1.
Acquire the object that you want to protect. When you want to access the protected object, first call System.Threading.Monitor.Enter(). Pass in the reference to the object to protect. ...

Get Microsoft® .NET Compact Framework Kick Start 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.