0

I am trying to prevent multiple copies of an application starting in C# on Windows 8.

bool isMutexNotExist;
var mutex = new Mutex(true, "name", out isMutexNotExist);

if (!isMutexNotExist)
{
     SystemSounds.Beep.Play();
     Application.Current.Shutdown();
}

mutex.WaitOne();
GC.KeepAlive(mutex);

This code works fine on Windows 7 (Dell Laptop) in Debug and Release mode (Visual Studio 2013) with .Net 4.5 but on Win 8 (Microsoft Surface 3) it lets another copy start. I couldn't find any reference to the Mutex's working different on Win7 & Win8 so i'm not really sure what is going on. The code didn't work on Windows 7 in release mode without the mutex.WaitOne().

user2939408
  • 153
  • 7
  • http://stackoverflow.com/q/14104988/1091693 – Score_Under Apr 29 '15 at 04:57
  • I'm aware of the various solutions that have been proposed. I have explictly tried the Mutex solution and it works on Win7 but not Win8 – user2939408 Apr 29 '15 at 05:26
  • In the answers to that question, it is using the return value of "WaitOne" on the mutex, rather than "isMutexNotExist". Does this approach change anything for you? There is also another approach mentioned involving WindowsFormsApplicationBase, does this work for you? – Score_Under Apr 29 '15 at 05:33
  • [GC.KeepAlive](https://msdn.microsoft.com/en-us/library/system.gc.keepalive%28v=vs.110%29.aspx) doesn't do what you seem to think it does. If you want that Mutex to not be collected whilst your application is running, you need to store it in a variable such as an instance field of an object that's definitely going to stay alive or in a static field, not a local variable. – Damien_The_Unbeliever Apr 29 '15 at 06:25

0 Answers0