In this final section, we will look at some general recommendations from Microsoft for working on multi-threaded applications. They include the following:
- Avoid using Thread.Abort to terminate other threads.
- Use a mutex, ManualResetEvent, AutoResetEvent, and Monitor to synchronize activities between multiple threads.
- Where possible, use a thread pool for your worker threads.
- If you have any worker threads that gets blocked, then use Monitor.PulseAll to notify all the threads of a change in the worker thread's state.
- Avoid using this, type instances, and string instances including string literals as lock objects. Avoid using types of the lock objects.
- Instance locks can result in deadlocks, so exercise caution when using them.
- Use the try/finally block with threads that enter a monitor so that in the finally block, you ensure that the thread leaves the monitor by calling...