Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C#
Article

A Single Instance Control Component in C#

Rate me:
Please Sign up or sign in to vote.
2.08/5 (10 votes)
1 Dec 20052 min read 105.6K   25   14
A Single Instance control component which checks whether any instance of your application is running on the system.

Introduction

This article is on a Single Instance control component which checks whether any instance of your application is running on the system. One can use this component to check whether a particular application is running on the system, or to avoid multiple instances of your application running on the system. There are many methods with which you can prevent multiple instances of your application, like using complex Mutex class methods or some unmanaged code.

Here, I have used a simple Process class to check whether a particular process is running or not. Let's first discuss the InstanceControl component. The InstanceControl class is derived from the Component class of the .NET Framework, and this component has a method called IsAnyInstanceExist() which checks for a particular process running on the system and returns the status as true/false.

  • Namespace: InstConLib
  • Class: InstanceControl
  • Members:
    • InstanceControl() (constructor which takes the process name)
    • IsAnyInstanceExist() (checks for the process and returns a bool value)

A brief description about the component members:

  1. The InstanceControl(string) is the constructor of the InstanceControl component. The constructor takes a single parameter string which is the process name and stores it in the member variable.
  2. The IsAnyInstanceExist() method of the InstanceControl component returns true/false by checking to see if the process is running or not. This method uses the Process class (alias System.Diagnostics.Process) and the GetProcessesByName() method which, in turn, returns the array of processes running with that name.
C#
using System;
using System.Diagnostics;
using System.ComponentModel;

namespace InstConLib { 
/* InstanceControlLib Class controls the instance */

  public class InstanceControl: Component
  {
    private string stProcName=null;
    /*constructor which holds the application name*/
    public InstanceControl(string ProcName)
    {
      stProcName=ProcName;
    }
    public bool IsAnyInstanceExist()
    {  /*process class GetProcessesByName() checks for particular
      process is currently running and returns array of processes
      with that name*/
      Process[] processes = Process.GetProcessesByName(stProcName);
      
        if(processes.Length != 1)
          return false; /*false no instance exist*/
        else 
          return true; /*true mean instance exist*/
    }
  } /*end of class*/
}/*end of namespace*/

Compile the above as a component library to produce a .dll file. Then, you can call this component in different clients like WinForms, WebForms or Console applications. I have used a simple console application to use this component. The client program calls the InstanceControl component and uses its method. In this example, I used two instances of the component. One which will check for the process that is not running, and the other which checks for the process which is running in the system.

Following is the code snippet of the client application:

C#
//InstClient.cs 
using System;
using InstConLib;
public class InstClient
{
  public static void Main()
  { 
   //First Object which looks for testApp.exe process  
   //remember its not neccessary to give extention of process
   InstConLib.InstanceControl in1 = new InstConLib.InstanceControl("testApp");
   if(in1.IsAnyInstanceExist())
    Console.WriteLine("Alreading one instance is running");
   else
    Console.WriteLine("No Instance running"); 
    //Second Object which looks for Explorer.exe process  
    //remember its not neccessary to give extention of process
    InstConLib.InstanceControl in2 = 
       new InstConLib.InstanceControl("Explorer");

   if(in2.IsAnyInstanceExist())
     Console.WriteLine("Alreading one instance is running");
   else
     Console.WriteLine("No Instance running");
 }
}

OUTPUT:

D:\vstudio>InstClient
No Instance running
Alreading one instance is running

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
United States United States
Chandra Hundigam-Venkat has Master degree in Computer Application, Microsoft Certified Professional and Cloud & Software Architect. Significantly involved in Cloud solutions, Cloud native application, Cloud migration, Enterprise application development distributed object oriented system development. Started computer career at the early age of eighteen as an intern in computer labs and had the opportunity to work with various USA fortune 500 companies in various technology and leadership roles.


Comments and Discussions

 
GeneralFirst instance not in the process list [modified] Pin
John.Jurisoo17-Sep-07 11:18
John.Jurisoo17-Sep-07 11:18 
GeneralNo client application code there!! Pin
mbthe2nd27-Nov-05 12:43
mbthe2nd27-Nov-05 12:43 
GeneralRe: No client application code there!! Pin
Chandra Hundigam Venkat2-Dec-05 6:41
Chandra Hundigam Venkat2-Dec-05 6:41 
GeneralProcess class bug in .NET Pin
allenLynx8-Sep-05 10:56
allenLynx8-Sep-05 10:56 
GeneralMultiple AppDomains in single process Pin
Tomaž Štih15-Apr-03 22:00
Tomaž Štih15-Apr-03 22:00 
GeneralRe: Multiple AppDomains in single process Pin
John Hills17-Apr-03 5:20
John Hills17-Apr-03 5:20 
GeneralMutex Pin
Adam Turner15-Apr-03 18:29
Adam Turner15-Apr-03 18:29 
GeneralRe: Mutex Pin
Tomaž Štih15-Apr-03 22:03
Tomaž Štih15-Apr-03 22:03 
GeneralRe: Mutex Pin
Alexander Werner17-Apr-03 5:14
Alexander Werner17-Apr-03 5:14 
GeneralRe: Mutex Pin
Sanju Burkule21-Apr-03 12:23
Sanju Burkule21-Apr-03 12:23 
GeneralRe: Mutex Pin
Mach0052-Apr-04 6:24
Mach0052-Apr-04 6:24 
GeneralRe: Mutex Pin
Requiem Sollar31-Jul-08 12:25
Requiem Sollar31-Jul-08 12:25 
GeneralNice Code Pin
Anonymous15-Apr-03 10:07
Anonymous15-Apr-03 10:07 
It's pretty easy to find if an instance of the app is running, but how do you find if a instance is running in the current user. Sometimes you can have an instance running already but its not in the same user (in Windows XP Fast user switching).

What do you do about application that have the same name as yours? Doesn't it return as a valid proc?
GeneralRe: Nice Code Pin
Alex Korchemniy17-Apr-03 4:46
Alex Korchemniy17-Apr-03 4:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.