As stated before, threads can be scheduled to run at any time and in any order. It often is necessary to coordinate various threads and to share resources among threads. One word of initial caution: All multithread applications work in debug mode; it's only in release mode when things start to go haywire. Debugging multithreaded applications takes time and experience; nothing else seems to help make life any easier when dealing with these multi-headed monsters. For some unknown and unexplainable reason, thread scheduling in Debug mode in no way represents the types of scheduling found in Release mode. Mainly, this is a side effect introduced by the debugging environment.
C# has the lock keyword. lock can be ...