PC Review


Reply
Thread Tools Rate Thread

AbandonedMutexException doesn't make sense.

 
 
dvestal@gmail.com
Guest
Posts: n/a
 
      5th Aug 2011
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();
}
}
}
}
}
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      6th Aug 2011
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
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
KB951978 Doesn't Make Sense Gis Bun Windows XP General 5 18th Jun 2008 02:59 PM
This doesn't make any sense Howard Windows Vista General Discussion 3 24th Mar 2006 02:16 PM
Odd exception that doesn't make sense to me =?Utf-8?B?YmFsbWVyY2g=?= Microsoft Dot NET 1 24th Feb 2006 07:28 AM
a StackOverflowException which doesn't make sense =?Utf-8?B?SmFrdWIgR2FqZGE=?= Microsoft C# .NET 2 31st Mar 2004 03:20 PM
World doesn't make sense (at least my IDE setup doesn't ;-)) Carlos Moreno Storage Devices 3 8th Jul 2003 10:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:30 PM.