Create Single Instance in Window application

Step 1: Create a Window Application.

Step 2: Go -> To-> Program.cs file



Step 3: Write this code in Main Method of Program.cs file.

//Add this line Program class.

private static Mutex m_Mutex;

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

    Application.EnableVisualStyles();

    Application.SetCompatibleTextRenderingDefault(false);

    //After that add below code

   bool createdNew;

   m_Mutex = new Mutex(true, "MyApplicationMutex", out createdNew);

   if (createdNew)

      Application.Run(new Form1());//Change the name of your startup form.

   else

      MessageBox.Show("The application is already running.", Application.ProductName,

      MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

} 

Step 4: After that build application and go -> Bin folder of your application and Go to-> Debug folder -> Click on Application exe.



Single Instance means -> at a time only one application window open.
 
Output
 
 
Ebook Download
View all
Learn
View all