Marco Morgia

Marco Morgia

  • 1.3k
  • 68
  • 1.2k

Mutex to prevent multiple program execution

Jul 3 2024 9:25 AM

Hi,

in my wpf application i  have to prevent te execution of two istance of the application. 
I've used mutex and all works as expected (if I try to start a second instance an error message will be displayed on screen). 

The problem became when I try to switch windows user (logon as administrator) and try to start application. In this case the application start normally without any message. And it's also possbile to start a second istance of the application for the same user (thing that not appen for the user when i start the first time the application).

Search some information I've read that in this condition (multiple users) it's necessary to create the mutex with "Global" key, but also in this manner I'm able to start the application without error message.

 

Below the code that I've used

  Mutex m = new Mutex(false, @"Global\App");

  if (!m.WaitOne(0,false))
  {
    //myApp is already running...
    MessageBox.Show("App is already running!", "Multiple Instances", MessageBoxButton.OK, 
                  MessageBoxImage.Error);

    System.Windows.Application.Current.Shutdown();
    return;
   }

 

Anyone had the same problem?


Answers (1)