D
dvestal
I have an app that grabs a named mutex as its first action. This is
used to ensure it's the only instance of the app running.
Sometimes the attempt to grab the mutex results in an
AbandonedMutexException, even though at that point there's only a
single thread, and the mutex hasn't even been grabbed yet. How is
this possible, and how do I recover from it?
Super-simplified code below:
using System;
using System.Diagnostics;
using System.Threading;
namespace TSSG.DeviceTesting
{
public class TestEngine
{
[STAThread]
static public void Main(string[] commandLineArgs)
{
TestEngine te = new TestEngine();
te.RunApplication(commandLineArgs);
}
public void RunApplication(string[] commandLineArgs)
{
Mutex mutex = null;
bool gotMutex = false;
try
{
// Ensure that this is the only
// instance of the app running.
mutex = new Mutex(
false,
"Opsys Engine Exclusivity");
// Sometimes this throws an AbandonedMutexException,
// despite being the first thing the app does.
// It seems to follow the operator killing the
// previous instance of the application using
// task manager. Is this possible? What's the
// best way to recover?
gotMutex = mutex.WaitOne(0, false);
if(!gotMutex)
{
MessageBox.Show(
"This application is already running.");
return;
}
// After this, several threads are created.
// Drivers are loaded, hardware is interacted
// with, etc. Crashes can occur anywhere.
// BUT...can this lead to an AbandonedMutex
// Exception for new invocations of the app?
DoLotsOfActualWork();
}
catch(Exception e)
{
}
finally
{
if(gotMutex && null != mutex)
{
mutex.ReleaseMutex();
}
}
}
}
}
used to ensure it's the only instance of the app running.
Sometimes the attempt to grab the mutex results in an
AbandonedMutexException, even though at that point there's only a
single thread, and the mutex hasn't even been grabbed yet. How is
this possible, and how do I recover from it?
Super-simplified code below:
using System;
using System.Diagnostics;
using System.Threading;
namespace TSSG.DeviceTesting
{
public class TestEngine
{
[STAThread]
static public void Main(string[] commandLineArgs)
{
TestEngine te = new TestEngine();
te.RunApplication(commandLineArgs);
}
public void RunApplication(string[] commandLineArgs)
{
Mutex mutex = null;
bool gotMutex = false;
try
{
// Ensure that this is the only
// instance of the app running.
mutex = new Mutex(
false,
"Opsys Engine Exclusivity");
// Sometimes this throws an AbandonedMutexException,
// despite being the first thing the app does.
// It seems to follow the operator killing the
// previous instance of the application using
// task manager. Is this possible? What's the
// best way to recover?
gotMutex = mutex.WaitOne(0, false);
if(!gotMutex)
{
MessageBox.Show(
"This application is already running.");
return;
}
// After this, several threads are created.
// Drivers are loaded, hardware is interacted
// with, etc. Crashes can occur anywhere.
// BUT...can this lead to an AbandonedMutex
// Exception for new invocations of the app?
DoLotsOfActualWork();
}
catch(Exception e)
{
}
finally
{
if(gotMutex && null != mutex)
{
mutex.ReleaseMutex();
}
}
}
}
}