We all must have observed some time or the other that when you try to open an application that is already running, you get a pop-up that says, “Another instance of this application is already running” or something like that.

This article will show how you can implement that in your Windows application using C#.

The main class used here is the Process class that is a part of the System.Diagnostics namespace. The Process class provides access to some local and also remote processes and can be used to start/stop these processes. Some static methods provided by the Process class are GetCurrentProcess() and GetProcessesByName(string process_name).

These methods pretty much do exactly what they are named.

NOTE: It is very important to note here that these methods simply get the already existing and running instances of the processes. They do not create new instances of the Process class. Note the words “gets/returns” in the preceding bullet points.

So to implement this functionality, all you need to do is, before you create a new instance of your application, get all the running processes and check the names of those processes with the currently running process. If you find such a process, it means that there is an instance already running and you should not create another instance for the same application. If not, just create a new instance for the application.

Check the following code. I have created a Windows Forms application with a very basic form with a label. In the Program.cs file, I created a static method that performs the preceding check and returns either true or false based on whether it finds a process with the same name or not.

[STAThread]
static void Main()
{
     if (AnotherInstanceExists())
     {
           MessageBox.Show("You cannot run more than one instance of this application.", "Only one instance allowed to run", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
           return;
     }
     Form1 objMyForm = new Form1();
     objMyForm.ShowDialog();
}
public static bool AnotherInstanceExists()
{
    Process currentRunningProcess = Process.GetCurrentProcess();
    Process[] listOfProcs = Process.GetProcessesByName(currentRunningProcess.ProcessName);
    foreach (Process proc in listOfProcs)
    {
       if ((proc.MainModule.FileName == currentRunningProcess.MainModule.FileName) && (proc.Id != currentRunningProcess.Id))
               return true;
    }
    return false;
}

The method AnotherInstanceExists() initially makes a call to GetCurrentProcess() to get a Process component associated with the currently running process. Then it makes a call to GetProcessesByName() and passes the currently running process's name as a parameter. With the array of Process components that are returned from this method, it checks whether the current process ID and process module's filename are the same. Basically it checks whether or not both of these processes are the same.

If the method returns true, the user is shown an error message that only one instance can be running for this application.

That's all there is to do. Please do provide your comments or suggestions to improve this article. Check the sample application that I have attached here.

I hope this helps!