I'm developing on an old codebase which has following code in several places:
Mutex mutex = new Mutex(false, "<some mutex name>");
mutex.WaitOne();
try {
// do something
} finally {
mutex.ReleaseMutex();
}
Those mutexes are used for intra-process synchronization so I rewrote them using locks instead and the problems seem to be gone.
I am aware that the code is not the best one out there (compared to What is a good pattern for using a Global Mutex in C#?), but that doesn't explain to me why mutex.ReleaseMutex() occasionally throws "Mutex is not owned".
So I'm asking what is missing in (or wrong with) the code above, that produces the exception?
ReleaseMutex
call shouldn't ever throw that exception. Are you sure that nothing in the "do something" code releases the mutex? Is the code always callingWaitOne
and notWaitOne(timeout)
? – Jim Mischel