On 8/5/2011 10:09 AM,
(E-Mail Removed) wrote:
> 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:
> 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?
http://msdn.microsoft.com/en-us/libr...ing.mutex.aspx says:
<quote>
Mutexes are of two types: local mutexes, which are unnamed, and named
system mutexes. A local mutex exists only within your process.
....
Named system mutexes are visible throughout the operating system, and
can be used to synchronize the activities of processes. You can create a
Mutex object that represents a named system mutex by using a constructor
that accepts a name.
</quote>
<quote>
If a thread terminates while owning a mutex, the mutex is said to be
abandoned. The state of the mutex is set to signaled, and the next
waiting thread gets ownership.
....
In the case of a system-wide mutex, an abandoned mutex might indicate
that an application has been terminated abruptly (for example, by using
Windows Task Manager).
</quote>
Arne