title: Force Single Instance with Mutex handling restart application
body: <p>I have a problem when i want use mutex to force single instance of my program. </p>
  
  <p>I a winform App with a WebBrowser Control. I need to autorestart if certain conditions are met. </p>
  
  <p>My Problem is that if i restart the application the mutex want let me open a new instance (this doesn't always happen) maybe cause browser has some async method like navigate. </p>
  
  <p>But i release the Mutex in Exit Application Event.</p>
  
  <p>This is my Code:</p>
  
  <pre><code>    static Mutex _mutex = new Mutex (false, "myprogramkey");
  
      [STAThread]
      private static void Main()
      { 
           Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
        if(_mutex.WaitOne(TimeSpan.FromSeconds(5), true))
        { 
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new RevolutionForm()); 
        }
  
       else
        {
            MessageBox.Show("Error!");
        }
      }
          static void Application_ApplicationExit(object sender, EventArgs e)
      {
          _mutex.ReleaseMutex();
      }
  </code></pre>