USE MUTEX CLASS PRACTICALLY TO HANDLE MULTIPLE PROCESSES

Before entering in to Mutex, I need to explain it's functional difference with Monitor class. This difference is still seeing as a hot discussion in side various communities. The difference is the scope between two types. Scope of Mutex is wider than Monitor. A mutex allow you to lock resources accessed by multiple processes while a monitor can only provides syncronisation mechanisms to multiple threads within a single process. So you can synchronize different Processes using Mutex and using Monitor you can do the same for different Threads in side a single Process. Below I used Mutex class for a practical purpose. As it can communicate with different Processes, I handled one real time scenario related to multiple processes.

Many users asking for single and only one instance of thier application at a time. An instance meant by a Process. So I thought to implement Mutex features to resolve this scenario. I created a class "HandleSingleInstance" for this purpose. User will create an object as like   

HandleSingleInstance single = new HandleSingleInstance ();

This will Initiate the constructor. I have certain lines inside this constructor like below.

public HandleSingleInstance()
        {
            Mutex mutex = null;
            try
            {
                mutex = Mutex.OpenExisting("SingleInstance");
            }
            catch
            {
                singleInstance = true;
            }
            if (mutex == null)
            {
                mutex = new Mutex(false, "SingleInstance");
                GC.KeepAlive(mutex);
            }
        }

I am trying to access a Mutex entity named "SingleInstance". 1st time you won't get this and it will move to "catch" block where I am setting local variable "singleInstance" as "true". More over I am defining mutex entity "SingleInstance" like mutex = new Mutex(false, "SingleInstance"). The line GC.KeepAlive(mutex) protexting our Mutex entity from garbage collector. So 1st time running our .EXE doing all these. What will happen, if you are trying to run another instance? Mutex entiy "SingleInstance" alreay set during 1st instance and you will get a meesage regarding this. Message added only for you reference. In real scenario you just need to kill the new process by    Process.GetCurrentProcess().Kill(). Below pasting the complete code.


using System;
using System.Diagnostics;
using System.Threading;
namespace ConsoleApplication1
{
    //Main class
    class Program
    {
       
        static void Main(string[] args)
        {
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Green;
            HandleSingleInstance single = new HandleSingleInstance();
            if (single.SingleInstance)
            {
                Console.WriteLine("1st instance running");
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey(false);
               
            }
            else
            {
                Console.WriteLine("Already running instance");
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey(false);
                Process.GetCurrentProcess().Kill();
            }
           
           
        }
    }
   
//Helper class for handling single instance
   public class HandleSingleInstance
    {
        private bool singleInstance = false;
        public bool SingleInstance
        {
            get { return singleInstance; }
        }
        public HandleSingleInstance()
        {
            Mutex mutex = null;
            try
            {
                mutex = Mutex.OpenExisting("SingleInstance");
            }
            catch
            {
                singleInstance = true;
            }
            if (mutex == null)
            {
                mutex = new Mutex(false, "SingleInstance");
                GC.KeepAlive(mutex);
            }
        }
    }
}
Ebook Download
View all
Learn
View all