title: Abandoned mutex exception
body: <p>I am trying to use a mutex for the first time and have the following code executing on two separate instances of the program</p>
  
  <pre><code>public void asynchronousCode()
      {
          using (var mutex = new Mutex(false, "mySpecialMutex"))
          {
              if (!mutex.WaitOne(1000, false))
              {
                  Console.WriteLine("First check - some one locked the mutex");
              }
  
              if (!mutex.WaitOne(3000, false))
              {
                  Console.WriteLine("Second check- some one locked the mutex");
              }
              else
              {
                  Console.WriteLine("I got the mutex");
                  Console.WriteLine("sleeping");
                  Thread.Sleep(3000);
                  Console.WriteLine("Awaking and Releasing mutex");
                  mutex.ReleaseMutex();
              }
          }
      }
  </code></pre>
  
  <p>When I run this, one of the instances (the one i run first) prints</p>
  
  <pre><code>I got the mutex
  sleeping
  awaking and releasing mutex
  </code></pre>
  
  <p>The other instance prints </p>
  
  <pre><code>First check - some one locked the mutex
  </code></pre>
  
  <p>and as soon as the first instance leases the mutex, it crashes at the second wait statement with the exception </p>
  
  <pre><code>The wait completed due to an abandoned mutex.
  </code></pre>
  
  <p>Any ideas on why I am getting this exception and how i can prevent it ? </p>
  
  <p>Solution: I probably should have read the mdsn documentation more clearly. Thanks Andrew for pointing me in the right direction</p>
  
  <p><em>You can use the WaitHandle.WaitOne method to request ownership of a mutex. The thread that owns a mutex can request the same mutex in repeated calls to WaitOne without blocking its execution. However, the thread must call the ReleaseMutex method the same number of times to release ownership of the mutex. The Mutex class enforces thread identity, so a mutex can be released only by the thread that acquired it.</em></p>