title: Using waitone() method
body: <pre><code>static Mutex mutex = new Mutex (false, "oreilly.com OneAtATimeDemo");
  
  static void Main()
  {
      // Wait a few seconds if contended, in case another instance
      // of the program is still in the process of shutting down.
  
      if (!mutex.WaitOne (TimeSpan.FromSeconds (3), false))
      {
        Console.WriteLine ("Another instance of the app is running. Bye!");
        return;
      }
  
      try
      {
        Console.WriteLine ("Running. Press Enter to exit");
        Console.ReadLine();
      }
      finally { mutex.ReleaseMutex(); }
  }
  </code></pre>
  
  <p><a href="http://www.albahari.com/nutshell/ch20.aspx" rel="noreferrer">http://www.albahari.com/nutshell/ch20.aspx</a></p>
  
  <p>In this code:</p>
  
  <pre><code>if(mutex.WaitOne(TimeSpan.Zero, true)) 
  {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new Form1());
      mutex.ReleaseMutex();
  } 
  else 
  {
      MessageBox.Show("only one instance at a time");
  }
  </code></pre>
  
  <p><a href="http://sanity-free.org/143/csharp_dotnet_single_instance_application.html" rel="noreferrer">http://sanity-free.org/143/csharp_dotnet_single_instance_application.html</a></p>
  
  <p>There is no inversion of the if/bool.</p>
  
  <p>If waitone() is true, does that mean an instance is already running? If true is returned, the current thread will be blocked which would mean two processes calling the same app will both halt?</p>
  
  <p>My understanding is as follows:</p>
  
  <pre><code>// Don't block the thread while executing code. 
  //Let the code finish and then signal for another process to enter
  </code></pre>
  
  <p>What is the implication of no ! (returning true) and vice versa. Or in other words, what happens either way?</p>
  
  <p>I know waitAll for example, waits for all threads to finish. Jon Skeet showed a good example of this on his site, which has stuck in my mind (credit to his explanations). So obviously waitOne waits for one thread to finish. The return value is what confuses me.</p>