Automatic Semaphore release on process Exit?

You can hook into the AppDomain. ProcessExit event to perform any cleanup operations like releasing the semaphore.

You can hook into the AppDomain. ProcessExit event to perform any cleanup operations like releasing the semaphore. Generally, named semaphores are designed to coordinate resources across processes without taking particular process life-time into account.

Semaphores in . NET are backed by native Windows semaphore objects, and the MSDN says: The semaphore object is destroyed when its last handle has been closed. Closing the handle does not affect the semaphore count; therefore, be sure to call ReleaseSemaphore before closing the handle or before the process terminates.

Hence the right approach is explicit handling before process termination. Update — Other options to consider: In case it's not feasible to handle “emergency” release manually in the AppDomain. ProcessExit event, consider creating an IDisposable wrapper that would acquire the semaphore in its constructor and release it in the Dispose method.

Another question is: is a Semaphore the right synchronization object for this case? Wouldn't a simple (named) mutex work better?

Your answer is OK, but I would like some magic to automatically clean up the semaphore 'raised' state for the process owning it upon exit or crash. – Daniel MoÅ¡mondor Mar 8 at 13:32 Edited to provide more options. – Ondrej Tucny Mar 8 at 13:41 Does named mutex have count?

I use this to limit number of program instances running (licensing) – Daniel MoÅ¡mondor Mar 8 at 13:44 No, they don't. In case you need count, Semaphores are the right option. For licensing, leveraging AppDomain.

ProcessExit seems as the way to go. Or, wrap the contents of your main method (except the licensing system bootstrap) into a using clause wrapping the semaphore handling — see option (1) at the end of my answer. – Ondrej Tucny Mar 8 at 18:38 Does the downvoter care to comment?

– Ondrej Tucny yesterday.

The appropriate answer is to implement a 'Critical Finalizer' around your semaphore to ensure proper clean-up in all cases. ProcessExit is not guaranteed to execute in a failure scenario, such as a forced appdomain unload due to non-trappable exceptions (StackOverflowException and InvalidProgramException being two good examples. ) More info @ msdn.microsoft.com/en-us/library/system...., to quote: "the common language runtime (CLR) guarantees that all critical finalization code will be given the opportunity to execute, provided the finalizer follows the rules for a CER, even in situations where the CLR forcibly unloads an application domain or aborts a thread.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions