4.8. Synchronize the Execution of Multiple Threads Using a Monitor
You need to coordinate the activities of multiple threads to ensure the efficient use of shared resources or to ensure several threads are not updating the same shared resource at the same time.
Identify an appropriate object to use as a mechanism to control access to the shared resource/data. Use the Shared method Monitor.Enter to acquire a lock on the object, and use the Shared method Monitor.Exit to release the lock so another thread may acquire it.
How It Works
The greatest challenge in writing a multithreaded application is ensuring that the threads work in concert. This is commonly referred to as thread synchronization and includes the following: