Why would the following structure cause an AbandonedMutexException. Even if there is an error or method returns. The mutex is being released.
static Mutex WriteMutex = new Mutex(false, @"Global\mutex2203");
public static void Demo()
{
try
{
WriteMutex.WaitOne();
//rest of coding stuff here
}
finally
{
WriteMutex.ReleaseMutex();
}
}
Receives reports cant regenerate the bug.
Edit: The exception occurs at WriteMutex.WaitOne();
no other code. And only this method touches that mutex.
An AbandonedMutexException
is thrown when one thread acquires a Mutex
object that another thread has abandoned by exiting without releasing it (see AbandonedMutexException). The code you cite in your question would not necessarily be the code that is causing the exception, only "receiving" it (i.e. detecting the situation that throws the exception).
That is, code in another thread (could be the same method but is likely not) acquires the Mutex
but does not release it and permits its thread to exit without the Mutex
ever being released. Then the thread running the code you show above throws the exception when it attempts to acquire the Mutex
.
You must also call WriteMutex.Dispose()
in the finally
block, but it is better to use a using
block.
Try to use this pattern:
https://stackoverflow.com/a/229567/2185689
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With