title: How to gracefully get out of AbandonedMutexException?
body: <p>I use the following code to synchronize mutually exclusive access to a shared resource between several running processes.</p>
  
  <p>The mutex is created as such:</p>
  
  <pre><code>Mutex mtx = new Mutex(false, "MyNamedMutexName");
  </code></pre>
  
  <p>Then I use this method to enter mutually exclusive section:</p>
  
  <pre><code>public bool enterMutuallyExclusiveSection()
  {
      //RETURN: 'true' if entered OK, 
      //         can continue with mutually exclusive section
      bool bRes;
  
      try
      {
          bRes = mtx.WaitOne();
      }
      catch (AbandonedMutexException)
      {
          //Abandoned mutex, how to handle it?
  
          //bRes = ?
      }
      catch
      {
          //Some other error
          bRes = false;
      }
  
      return bRes;
  }
  </code></pre>
  
  <p>and this code to leave it:</p>
  
  <pre><code>public bool leaveMutuallyExclusiveSection()
  {
      //RETURN: = 'true' if no error
      bool bRes = true;
  
      try
      {
          mtx.ReleaseMutex();
      }
      catch
      {
          //Failed
          bRes = false;
      }
  
      return bRes;
  }
  </code></pre>
  
  <p>But what happens is that if one of the running processes crashes, or if it is terminated from a Task Manager, the mutex may return <code>AbandonedMutexException</code> exception. So my question is, what is the graceful way to get out of it? </p>
  
  <p>This seems to work fine:</p>
  
  <pre><code>    catch (AbandonedMutexException)
      {
          //Abandoned mutex
          mtx.ReleaseMutex();
          bRes = mtx.WaitOne();    
      }
  </code></pre>
  
  <p>But can I enter the mutually exclusive section in that case?</p>
  
  <p>Can someone clarify?</p>