Restricting a C# Application to a Single Instance

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.

  • GetCurrentProcess() basically gets a Process component and links it to the current running process from which this method was called. That means that if we call this method from our app (which is what we'll do shortly), the method returns a Process component that is linked to our app's process.
     
  • GetProcessesByName(string process_name) returns an array of Process components that are currently running in the local system with the specified process name, process_name. So for example, if you have the Chrome browser running with multiple tabs open, then you can see in the Task Manager that there are as many number of “Chrome.exe” processes running as there are tabs in your browser. Now if you call this method for “Chrome.exe”, you will get an array of Process components each having all the details of one chrome.exe process.

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!

 

Up Next
    Ebook Download
    View all
    Learn
    View all