Prevent Users From Running More Than One Instance of a Program-C#

By using Mutex the object you can prevent users from running more than one instance of the application. Check out this code snippet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace SingleInstacle
{
    public class Program
    {
        static void Main(string[] args)
        {
            bool createdNew = false;
            Mutex mutex = new Mutex(true, "SingleInstance", out createdNew);

            if (createdNew)
            {
                Console.WriteLine("Hello world");

            }
            else
            {
                Console.WriteLine("You can only run a single instance of this app!");
            }
            Console.ReadLine();
        }
    }
}

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post