Or a different title:
Why disposing an acquired Mutex is destroying it?
I have the following code, the real one spawns between several methods and does things where this one sleeps:
bool createdNew;
using (Mutex mutex = new Mutex(true, "Global\\AAA", out createdNew))
{
if (!createdNew)
{
throw new InvalidOperationException();
}
}
Thread.Sleep(15000);
using (Mutex mutex = new Mutex(false, "Global\\AAA", out createdNew))
{
if (!createdNew)
{
mutex.ReleaseMutex();
}
else
{
throw new InvalidOperationException();
}
}
I was expecting the first time to get a createdNew = true and the second time a false but I get a true both times.
It is related with disposing, If I don't dispose the Mutex
then everything works as expected but as I found in several places like here disposing a Mutex
does not release it (so I guess it does not destroy it as well as the Mutex
is acquired by the current thread).
So again, why disposing is destroying an acquired Mutex
?