I am using a Mutex
object to prevent multiple instances of the application from running at the same time.
This is the relevant code:
// class attribute
private static Mutex applicationMutex = new Mutex(true, "MyProgram");
// Main()
try
{
if (!applicationMutex.WaitOne(TimeSpan.Zero, true))
{
MessageBox.Show("Already running.");
Application.Current.Shutdown();
}
}
catch (AbandonedMutexException)
{
// pass
}
It seems to work fine, but I noticed that sometimes it seems to "hang": for example, if I terminate the program while debugging using the "stop" button in Visual Studio I'm usually unable to relaunch my application unless I disconnect my user and log back in. This morning it even happened after a reboot!
What am I doing wrong?