c# - Is it OK to have an empty while block when using WaitOne with named mutexes for IPC? -
basically, multiple instances of same process this:
using (var mutex = new system.threading.mutex(false, mutex_name)) { while (!mutex.waitone(100)) { /* wait acquire mutex other process */ } try { dosynchronizedwork(); } { mutex.releasemutex(); } }
will there pitfalls? can process exit without releasing mutex? abandonedmutexexception
thrown if happens? or process acquire mutex? else?
edit:
looks correct way should (at least scenario) this:
using (var mutex = new system.threading.mutex(false, mutex_name)) { bool lockobtained = false; while (!lockobtained) { try { while (!mutex.waitone()) { /* wait acquire mutex */ } lockobtained = true; } catch (abandonedmutexexception ignored) { // mutex abandoned // process before process completed. // can try obtain again. } } try { dosynchronizedwork(); } { mutex.releasemutex(); } }
what's point of this? wait without timeout.
when process exits resources cleaned up. abandonedmutexexception
must assumed able happen in ipc scenarios because other process might fail @ time (due bug or out of memory , such).
in case doing thread.abort
works: thread.abort
super dangerous. leave comment , i'll able migrate better.
Comments
Post a Comment