Along with Juan's answer, there's a difference in terms of garbage collection between launching inside and outside a debugger. That's not quite the same as a Debug configuration vs a Release configuration, but it's something you should be aware of anyway.
In the debugger, a local variable will act as a GC root for its entire scope - but when you're not debugging, your mutex
variable isn't a GC root at all, because you don't use it after initializing it. That means your Mutex
can be garbage collected (and thus the native mutex will be released) pretty much immediately.
You should use a using
statement to explicitly dispose the Mutex
at the right time:
// Note that you don't need a variable here... you can have one if you
// want though
using (new Mutex(...))
{
// Code here to be executed while holding the mutex
}