Hello,

I wonder how we can use lock objects between 2 different applications.
In the example below we can use lock objects to let only one thread to execute the code inside the lock object at a time.
This works fine within one application.

Now I wonder how we can use a lock object between 2 different applications. Is there some kind of Global system lock that
can be used?

I have put this code as illustration of what I try to find a solution for:

        object lockobject = new object();

        //Application 1
        void thread1()
        {
            Random rnd = new Random();
            while (true)
            {
                lock (lockobject)
                {
                    //Allow only 1 application at a time in this code area!
                }
                Thread.Sleep(rnd.Next(20, 2000));
            }
        }

        //Application 2
        void thread2()
        {
            Random rnd = new Random();
            while (true)
            {
                lock (lockobject)
                {
                    //Allow only 1 application at a time in this code area!
                }
                Thread.Sleep(rnd.Next(20, 2000));
            }
        }

Each application has it's objects in it's own memory, so the same locking mechanism can't work.

There are ways for applications to share memory, but I haven't used that in ages.

Yes, I am trying to read about "named mutexes". That seems to be a solution to interprocess between for example 2 application where the "named mutex" can be accessible "computer wide" and then with other words by 2 different applications.

However I have never used it so I am not 100% sure of the fact above?
I found an example on this url:
"http://www.c-sharpcorner.com/UploadFile/1d42da/threading-with-mutex/"

The example shows to Wait and Release a named mutex like below. I wonder if this is the correct way to do it and that the mutex is accessible from 2 different applications?

        //Application 1
        void thread1()
        {
            Random rnd = new Random(); Mutex mutex = new Mutex(false, "RunsMutex");
            while (true)
            {
                mutex.WaitOne();
                //Allow only 1 application at a time in this code area!

                //release the mutex
                mutex.ReleaseMutex();
                Thread.Sleep(rnd.Next(1, 10));
            }
        }

        //Application 2
        void thread2()
        {
            Random rnd = new Random(); Mutex mutex = new Mutex(false, "RunsMutex");
            while (true)
            {
                mutex.WaitOne();
                //Allow only 1 application at a time in this code area!

                //release the mutex
                mutex.ReleaseMutex();
                Thread.Sleep(rnd.Next(1, 10));
            }
        }

Maybe you could use a 1 byte file and open it for exclusive access for use as a system-wide lock?

The problem is that I am using the file lock technique now but this is very slow as you need to use Thread.Sleep(1). When using 1 millisecond sleep, that in practic isn't 1 millisecond as the kernel can wait up to half a second.

The ideá is good but the Sleep(1) and by using the catch{} are making it very slow if there is not any other way to improve the speed with this code?

            String lockfile = "C:/lockfile.txt"; FileStream lockfilestream = null;
            while (true)
            {
                try
                {
                    //Try to Lock the symbol file
                    lockfilestream = new FileStream(lockfile, FileMode.Open, FileAccess.Read, FileShare.None);
                    break;
                }
                catch { }
                Thread.Sleep(1);
            }
            ///
            ///
            //Critial code area
            ///
            ///
            if (lockfilestream != null) { lockfilestream.Close(); }

That sleeps even if the file is available. Maybe better like

while (true) {
  try {
     get filestream
     break;
  } catch ... {
     sleep 1
  }
)

>> That sleeps even if the file is available. Maybe better like

I must ask, I don't think above is true as I am using "break" when it succeds and by this breaking out of the loop Before Thread.Sleep(1); ?

Oh. Yes. You're right. My mistake - I apologise.

Maybe it's slow because you are thrashing the file system with 1000 requests per second. Depending on your latency requirements you may find that it's a lot faster overall with a sleep of (eg) 100mSec

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.