Mutex is not released even after I released it

I have a number of services that read xml files. To make sure there are no collisions, I use a mutex. For any reason, if all my services are running by the same user, there is no problem. However, if there are different users running these services, even after one service has released the mutex, the other gets the following exception when calling enter route mutex "Unhandled Exception: System.TypeInitializationException: the type initializer for" createMutex.Program "threw an exception. - -> System.UnauthorizedAccessException: access to the path "RETEST_MUTEX" is denied.

public static readonly String ROUTE_MUTEX_STRING = "RETEST_MUTEX"; private static Mutex _routeMutex = new Mutex(false, ROUTE_MUTEX_STRING); /// <summary> /// Thin wrapper around the static routeMutex WaitOne method /// Always call ExitRouteMutex when done in protected area /// </summary> /// <param name="millis_timeout"></param> /// <returns>true if signaled, like WaitOne</returns> public static bool EnterRouteMutex(int millis_timeout) { try { return _routeMutex.WaitOne(millis_timeout, false); } catch (AbandonedMutexException ame) { // swallow this exception - don't want to depend on other apps being healthy - like pre .NET 2.0 behavior // data integrity will be checked return _routeMutex.WaitOne(millis_timeout, false); } } public static void ExitRouteMutex() { try { _routeMutex.ReleaseMutex(); } catch (ApplicationException) { // swallow, reduce complexity to client } } static void Main(string[] args) { Console.WriteLine("Start"); bool get = EnterRouteMutex(1000); System.Console.WriteLine("Mutex created Press enter " + get.ToString()); Console.ReadLine(); ExitRouteMutex(); Console.WriteLine("Mutex Release"); System.Console.WriteLine("Press enter"); Console.ReadLine(); } 
+4
source share
1 answer

Here is an example of running a mutex cross process.

http://msdn.microsoft.com/en-us/library/c41ybyt3.aspx

It handles the use of Mutex.OpenExisting, and also demonstrates the security aspect mentioned by cdhowie.

+3
source

All Articles