Holding a lock during a call to System.Threading.Monitor.Wait() can lead to performance problems or deadlock because the lock can prevent other threads from running. This can be caused by nesting the call to System.Threading.Monitor.Wait() in two lock statements, or by waiting on a different object to the one which is locked.

Synchronized methods (with the attribute [MethodImpl(MethodImplOptions.Synchronized)]) can also cause problems, because they are equivalent to lock(this) or lock(typeof(Class)).

Ensure that the call to System.Threading.Monitor.Wait() occurs within a single lock statement and ensure that the argument to System.Threading.Monitor.Wait() matches the variable in the lock statement.

The following example locks two variables, countLock and textLock, then calls System.Threading.Monitor.Wait(). However for the duration of Wait(), the variable countLock is locked, which deadlocks the program.

The problem can be fixed by moving the lock statement so that countLock is not locked for the duration of the wait.

  • MSDN, C# Reference: lock Statement.