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:
- 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.
- 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.
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace InstConLib {
public class InstanceControl: Component
{
private string stProcName=null;
public InstanceControl(string ProcName)
{
stProcName=ProcName;
}
public bool IsAnyInstanceExist()
{
Process[] processes = Process.GetProcessesByName(stProcName);
if(processes.Length != 1)
return false;
else
return true;
}
}
}
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:
using System;
using InstConLib;
public class InstClient
{
public static void Main()
{
InstConLib.InstanceControl in1 = new InstConLib.InstanceControl("testApp");
if(in1.IsAnyInstanceExist())
Console.WriteLine("Alreading one instance is running");
else
Console.WriteLine("No Instance running");
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