Run Only One Copy Of Application

Posted by RaviRanjanKr under Windows Forms category on | Points: 40 | Views : 3521
This Code Snippet allow you to run only one Instance of application at a time.
Many time we have requirement that we need to run only one instance of application at a time. If you have same requirement then in that case this Code Snippet might be useful for you.
Here you will learn how to handle only one instance of application by using Mutex

// This is default Program.Cs where Entry point (Main method) lies
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace OnlyOneInstance
static class Program
static void Main()
bool instantiated;
/* If instantiated is true, this is the first instance of the application; else, another instance is running. */
Mutex mutex = new Mutex(true, "UniqueID", out instantiated);
if (!instantiated)
MessageBox.Show("Already Running");
Application.Run(new Form1());

You can download Source code from given link to getting more details

After downloading Go to bin directory and then debug OnlyOneInstance.exe file twice, you will see a message box which stated Program Already Running .

Comments or Responses

Login to post response