Ensure That Only One Instance of an Application Can Execute Concurrently - CSharp Thread Asynchronous

CSharp examples for Thread Asynchronous:Mutex

Description

Ensure That Only One Instance of an Application Can Execute Concurrently

Demo Code


using System;// ww w.  j  a  va 2  s .  c  o  m
using System.Threading;

class MainClass
    {
        public static void Main()
        {
            bool ownsMutex;
            using (Mutex mutex = new Mutex(true, "MutexExample", out ownsMutex))
            {
                if (ownsMutex)
                {
                    Console.WriteLine("This application currently owns the mutex.");
                    Console.WriteLine("Press any key.");
                    Console.ReadLine();

                    mutex.ReleaseMutex();
                }
                else
                {
                    Console.WriteLine("Another instance of this application already owns the mutex named MutexExample. ");
                }
            }
        }
    }

Result


Related Tutorials