0

I am trying to set up a mutex to prevent my application launcher executable from starting several identical processes. I have seen several people using similar code with Application.Run() instead of Process.Start(), where one blocks while the other does not. My code seemingly does what it should, but I do not understand why. The application launcher exits shortly after the code below, so would the mutex not be disposed?

// Prevent multiple application instances by checking our global mutex.
        using (var mutex = new Mutex(false, @"Global\" + ApplicationGuid))
        {
            if (!mutex.WaitOne(0, false))
            {
                MessageBox.Show("An instance of this application is already running.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Start application.
            Process.Start(applicationPath);
        }
Excavator
  • 38
  • 3
  • 2
    The `Mutex` object encapsulates a handle to a mutex kernel object. The object does not disappear merely because a handle to it is closed -- they disappear only when *all* handles to them have been closed. – Jeroen Mostert Feb 08 '18 at 14:24
  • Can you clarify what is the question? `Application.Run()` and `Process.Start()` do completely different things. – Leonid Vasilev Feb 08 '18 at 14:25
  • I apologize if the question is unclear. What I want to know: When this code exits and runs again, why does it not trigger the MessageBox.Show()? My understanding is that the mutex would have been released and not block the next time. @JeroenMostert Thank you, that is useful information! If I understand correctly, the mutex will block next time around as long as the started process has a handle during its execution. – Excavator Feb 08 '18 at 14:30

0 Answers0