1

I am trying to achieve synchronization between different processes. Multiple process calling ProcessLock should be synchronized. I am able to achieve it. But problem here is that Other threads are unable to enter critical section. Lock is always acquired by same application. How can I share the lock between different application.

  public class ProcessLock : IDisposable
        {
            // the name of the global mutex;
            private const string MutexName = "FAA9569-7DFE-4D6D-874D-19123FB16CBC-8739827-[SystemSpecicString]";

            private Mutex _globalMutex;

            private bool _owned = false;

           // Synchronized constructor using mutex    
            public ProcessLock(TimeSpan timeToWait)
            {
                try
                {
                    _globalMutex = new Mutex(true, MutexName, out _owned);
                    if (_owned == false)
                    {
                        // did not get the mutex, wait for it.
                        _owned = _globalMutex.WaitOne(timeToWait);
                    }
                }
                catch (Exception ex)
                {
                    Trace.TraceError(ex.Message);
                    throw;
                }
            }

            public void Dispose()
            {
                if (_owned)
                {   
                    //Releasing  the lock to be acquired by different processes.
                    _globalMutex.ReleaseMutex();
                }            
                _globalMutex = null;
            }
        }

If three methods are calling this constructor all should call sequentially or some round robin fashion.

I have following wrapper class

 public class CrossProcessLockFactory
    {
        private static int DefaultTimoutInMinutes = 2;
        public static IDisposable CreateCrossProcessLock()
        {
            return new ProcessLock(TimeSpan.FromMinutes(DefaultTimoutInMinutes));
        }

        public static IDisposable CreateCrossProcessLock(TimeSpan timespan)
        {
            return new ProcessLock(timespan);
        }
    }

And inside main method.

 using (CrossProcessLockFactory.CreateCrossProcessLock())
            {
                // if we get out it is ready
                Console.WriteLine("Using the mutex on process 1. Press any key to release the mutex");
                Console.ReadLine();
            }
Manjay_TBAG
  • 2,176
  • 3
  • 23
  • 43
  • So problem is multiple processes cannot enter ProcessLock at the same time, but multiple threads of the same process can? – Evk Dec 28 '16 at 10:58
  • Yes. But I have single thread in each process which calls this method in a loop. – Manjay_TBAG Dec 28 '16 at 11:00
  • Can you show how exactly are you using this ProcessLock class? – Evk Dec 28 '16 at 11:03
  • I have edited my question to show the use. i gave this as an example. My actual implementation is here in this stackocverflow question. http://stackoverflow.com/questions/41356577/issue-with-mutex-getting-lock-before-it-was-released-and-how-to-distribute-locks – Manjay_TBAG Dec 28 '16 at 11:10
  • Still don't understand the problem. You are saying you have single thread in each process which calls this method in a loop. In this case it works as expected - while one process is inside your using block - another process is blocked and cannot enter it. – Evk Dec 28 '16 at 11:14
  • I have 3 different exes running, all of them having this using block say in a while true loop. But CreateCrossProcessLock is only accessed by one of exes others are not getting chance to execute this method. Please let me know if I am unable to explain this to you. – Manjay_TBAG Dec 28 '16 at 11:47
  • 1
    I cannot reproduce the problem with the code you provided. I removed Console.ReadLine and put Thread.Sleep(2000) instead, started 3 processes and they acquire mutex sequentially one by one. – Evk Dec 28 '16 at 12:25
  • there are some smells in the code (for example why overarchitecturing it with factory?), consider better code from here: http://derpturkey.com/c-cross-process-synchronization/ – Andrzej Martyna Apr 17 '18 at 11:54

0 Answers0