Skip to main content

Create an application mutex (single running) in C#

using System;
using System.Reflection;
using System.Threading;

class Program
{
    public static void Main()
    {
        // Obtain the mutex name from the full assembly name.
        string mutexName = Assembly.GetEntryAssembly().FullName;

        // firstApplicationInstance indicates whether this is the first application instance.
        using (Mutex mutex = new Mutex(false, mutexName, out bool firstApplicationInstance))
        {
            if (!firstApplicationInstance)
            {
                Console.WriteLine("This application is already running.");
            }
            else
            {
                Console.WriteLine("ENTER to shut down");
                Console.ReadLine();
            }
        }
    }
}