0

does calling Dispose() on an acquired named system mutex (eg via using in code below) release the mutex?

using (var m = new Mutex(false, "name")) {
    m.WaitOne();
}

EDIT: is the behavior documented? from my disassembly Dispose() is handled by the SafeWaitHandle base class of Mutex and i'm unclear how this translates.

CoderBrien
  • 663
  • 1
  • 7
  • 22
  • 3
    Yes, `Mutex.Dispose` releases the mutex. I suspect you could have checked that for yourself though... – Jon Skeet Mar 06 '17 at 22:37
  • @JonSkeet i'm seeing some weird inconsistent behavior and was unable to locate actual documentation as to the behavior. do you have a reference other than actual experience? – CoderBrien Mar 06 '17 at 22:38
  • 1
    @CoderBrien Google is a *fantastic* resource when it comes to finding documentation on...basically any programming related concept out there. – Servy Mar 06 '17 at 22:41
  • @servy i would't have asked if i didn't already try to find the answer on google. sheesh. – CoderBrien Mar 06 '17 at 22:42
  • @servy here is the main documentation page of `Mutex`. i challenge you to show me where the behavior is documented. https://msdn.microsoft.com/en-us/library/system.threading.mutex(v=vs.110).aspx – CoderBrien Mar 06 '17 at 22:44
  • Well, the documentation says it "releases all resources". It could be clearer, certainly - but it's at least pretty suggestive. If you've had experiences that suggest otherwise, those should be detailed in the question, ideally with a [mcve]. – Jon Skeet Mar 06 '17 at 22:47
  • [This answer](http://stackoverflow.com/a/25433239/563532) seems to indicate that the mutex is *not* released on dispose. – Rob Mar 06 '17 at 22:50
  • @rob thank you sir. – CoderBrien Mar 06 '17 at 22:52
  • @rob if you create an answer with a link to that question i will accept it. – CoderBrien Mar 06 '17 at 22:53
  • @JonSkeet that suggestive documentation is inherited from the base class and turns out to be the wrong interpretation. whatdoyaknow! – CoderBrien Mar 06 '17 at 22:56
  • That suggests it should be easy to demonstrate, no? (And you absolutely should *not* accept an answer that is just a link to another one. If you believe this is a duplicate question, it should be closed as a duplicate.) – Jon Skeet Mar 06 '17 at 23:02
  • @JonSkeet it's a dup. and the other answer is correct. and i shouldn't have to demonstrate something that should be documented and isn't. and your answer was baseless. – CoderBrien Mar 06 '17 at 23:03
  • You should have provided evidence that it wasn't how you expected it to be, because that's part of the diagnostic work before you ask a question. Have closed this now as a dupe. (But yes, I acknowledge that my original comment - not an answer - appears to be incorrect. That still surprises me...) – Jon Skeet Mar 06 '17 at 23:04
  • @JonSkeet don't fret, nobody is perfect. your answer was wrong. – CoderBrien Mar 06 '17 at 23:05

0 Answers0