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();
        }
    }
}

Post a Comment

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

Previous Post Next Post

Blog ads

CodeGuru