![]() |
Languages »C / C++ Language »General IntermediateA Single Instance Control Component in C#ByChandra HundigamA Single Instance control component which checks whether any instance of your application is running on the system. | C#, VC7.NET 1.0, Win2K, WinXP, Visual Studio, Dev
|
Advanced Search |
|
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 simpleProcess
class to check whether a particular process is running or not. Let's first discuss theInstanceControl
component. TheInstanceControl
class is derived from theComponent
class of the .NET Framework, and this component has a method calledIsAnyInstanceExist()
which checks for a particular process running on the system and returns the status astrue
/false
.
InstConLib
InstanceControl
InstanceControl()
(constructor which takes the process name)IsAnyInstanceExist()
(checks for the process and returns abool
value)A brief description about the component members:
InstanceControl(string)
is the constructor of theInstanceControl
component. The constructor takes a single parameter string which is the process name and stores it in the member variable.IsAnyInstanceExist()
method of theInstanceControl
component returnstrue
/false
by checking to see if the process is running or not. This method uses theProcess
class (aliasSystem.Diagnostics.Process
) and theGetProcessesByName()
method which, in turn, returns the array of processes running with that name.using System;using System.Diagnostics;using System.ComponentModel;namespace InstConLib {/* InstanceControlLib Class controls the instance */publicclass InstanceControl: Component {privatestring stProcName=null;/*constructor which holds the application name*/public InstanceControl(string ProcName) { stProcName=ProcName; }publicbool 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)returnfalse;/*false no instance exist*/elsereturntrue;/*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 theInstanceControl
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:
//InstClient.csusing System;using InstConLib;publicclass InstClient{publicstaticvoid 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"); }}
D:\vstudio>InstClientNo Instance runningAlreading one instance is running
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
General
News
Question
Answer
Joke
Rant
Admin
PermaLink |Privacy |Terms of Use Last Updated: 1 Dec 2005 Editor:Smitha Vijayan | Copyright 2003 by Chandra Hundigam Everything elseCopyright ©CodeProject, 1999-2009 Web11 |Advertise on the Code Project |