Single Instance Control Component

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 in many ways like to check whether a particular application is running on the system or to avoid multiple instance 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 method or some unmanaged code. Here I have used simple Process class to check for the particular process is running or not.

Lets discuss first about InstanceControl component, InstanceControl Class is derived from Component class of .net frame work and this component  is having 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<u1:p></u1:p>

Members:  1. InstanceControl() (Constructor which takes the process name)<u1:p> </u1:p>

      2. IsAnyInstanceExist() ( Checks for the process and returns bool value)<u1:p> </u1:p>

     

Brief Description about the component members:<u1:p> </u1:p>

1.The InstanceControl(string) is a constructor of the InstanceControl component. Constructor take single parameter string which is the process name and store it the member variable.

2. The IsAnyInstanceExist() method of InstanceControl component returns true/false by checking the Process running or not. This method uses Process class (alias System.Diagnostics.Process) GetProcessesByName() method which in turn returns the array of processes running with that name. 

using System;
using System.Diagnostics;
using System.ComponentModel;
namespace InstConLib 

        /// <summary> 
        /// InstanceControlLib Class controls the instance . 
        /// </summary> 

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 component library to produce .dll file then one can call this component in different clients like WinForms, WebFroms or Console applications.I have used simple console application to use this component.<u1:p></u1:p>  

The client program calls the InstanceControl component and uses its method. In this example I used to instance of component one which will check for the process that is not running and other which checks for the process which is running in the system. <o:p></o:p>

<u1:p>

class="
smallblack">Following is the code snippet  of the client application</u1:p> <o:p></o:p>

//InstClient.cs<o:p></o:p>

using System;
using InstConLib;
public class InstClient
{
  public static void Main()
  {
<o:p></o:p>

  //First Object which looks for testApp.exe process <o:p></o:p>

 //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");
<o:p></o:p>

  //Second Object which looks for Explorer.exe process <o:p></o:p>

 //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

Further reading <u1:p></u1:p>

  1. .Net  More information on .Net technologies

 About the Author: Chandra Hundigam has Master degree is Computer Application, Working on Microsoft Technologies for past 5 years. Presently working as consultant for a US based company. His area of interests is Emerging Technologies.You can reach him at chanduhv@hotmail.com

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!