title: How to create a safe non-global Mutex for forms launched asynchronously
body: <p><strong>Requirement:</strong></p>
  
  <p><strong>A.</strong> 1 user login can open 0-1 <code>System.Windows.Form</code> instances on 1 physical machine in <code>ProcessName</code>.<br>
  <strong>B.</strong> 2 different user logins are each able to open 0-1 <code>System.Windows.Form</code> instances on 1 physical machine in <code>ProcessName</code>. <br />
  <strong>C.</strong> <code>MutexChecker</code> is in its own class rather than a member of <code>Form</code> because the responsibility for  disposing of the Mutex changes from one form to a different form based on user actions.<br />
  <strong>D.</strong> Said <code>Form</code>'s are launched asynchrously after the creation of the Mutex.  So I cannot implement a <code>using</code> statement.</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>On other SO posts, I've seen code similar to the below labeled as "unsafe".  However, the reasons for the code being labeled "unsafe" are not given.  Rather, a <a href="https://stackoverflow.com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c/229567">global mutex</a> is offered as the solution.  A global mutex works machine-wide, therefore it does not meet the requirement.</p>
  
  <p><strong>What I have tried:</strong></p>
  
  <p>Call <code>mutexChecker.RunCheck()</code> from said <code>System.Windows.Form</code></p>
  
  <pre><code>public class MutexChecker:IDisposable
  {
      private Mutex mutex;
  
      public bool RunCheck()
      {
          bool createdNew;
          mutex = new Mutex(initiallyOwned: true, name: "AppName", createdNew: out createdNew);
  
          if (!createdNew)
          {
              MessageBox.Show("AppName is already running.  Please complete the other note before opening a new window.");
          }
  
          return createdNew;
      }
  
      public void Dispose()
      {
          mutex.ReleaseMutex();//ERROR: safe handle has been disposed
          mutex.Dispose();//Does not always release the mutex for some unknown reason
      }
  </code></pre>
  
  <p>Dispose on the mutex during <code>Form.Dispose</code>.</p>
  
  <p><strong>Question</strong></p>
  
  <p>How can I safely create a mutex that fulfills the requirement?</p>