c# - Mutex.waitOne always returns False on particular machine -
i totally stumped here. have chunk of code relies on mutex protect it. when run chunk of code on development machine , test machine works fine. when force production machine fails.
but here's kicker, used work , didn't create code changes particular block. wait there's more! have sec process uses literally exact same block of code , works fine!
the mutex declared such:
mutex _mutex = new mutex(false, "sendtextmessage_11a52b63-4fc6-46df-b72c-c45b225d4143");
the block of code using this:
public override void forwardtextmessagestodevice() { trace.writeline("here1"); if (!_mutex.waitone(30000)) { trace.writeline("here2"); trace.writeline(string.format("return mutex forwardtextmessage: {0} {1}", this.name, datetime.now)); return; } seek { trace.writeline("here3"); scheduledreports(); } { trace.writeline("here4"); _mutex.releasemutex(); }
the trace output this:
here1 here2 homecoming mutex forwardtextmessage: method 2/11/2013 3:59:06 pm here1 here2 homecoming mutex forwardtextmessage: method 2/11/2013 4:00:06 pm
the mutex appears locked right out of gate. i'm under impression mutex's shared between threads , destroyed when process exits 2 separate processes sharing same mutex should good, right? if application crashed before mutex released cause behavior?
edit: forgot. randomly application had crashed hard , had forcefulness quit it.
the mutex appears locked right out of gate. i'm under impression mutex's shared between threads , destroyed when process exits 2 separate processes sharing same mutex should good, right?
no. named mutex instances shared between processes. process can have mutex locked.
from documentation of mutex constructor using:
mutexes of 2 types: local mutexes , named scheme mutexes. if create mutex object using constructor accepts name, associated operating-system object of name. named scheme mutexes visible throughout operating system, , can used synchronize activities of processes.
as sec question:
also if application crashed before mutex released cause behavior?
this covered documentation mutex:
if thread terminates while owning mutex, mutex said abandoned. state of mutex set signaled , next waiting thread gets ownership. if no 1 owns mutex, state of mutex signaled. origin in version 2.0 of .net framework, abandonedmutexexception thrown in next thread acquires mutex. prior version 2.0 of .net framework, no exception thrown.
in scenario application terminates, mutex should flagged abandoned. attempting acquire mutex
should raise abandonedmutexexception
.
c# .net service mutex
No comments:
Post a Comment