Movatterモバイル変換


[0]ホーム

URL:


Wayback Machine
7 captures
14 Apr 2008 - 26 Sep 2009
MarSEPOct
Previous capture26Next capture
200820092010
success
fail
COLLECTED BY
Organization:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
Collection:alexa_web_2009
this data is currently not publicly accessible.
TIMESTAMPS
loading
The Wayback Machine - https://web.archive.org/web/20090926095546/http://www.codeproject.com:80/KB/cpp/singleinstcs.aspx
Click here to Skip to main content
6,490,150 members and growing! (18,799 online)
EmailPassword helpLost your password?
The Code Project
Languages »C / C++ Language »General    Intermediate

A Single Instance Control Component in C#

ByChandra Hundigam

A 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
Posted:14 Apr 2003
Updated:1 Dec 2005
Views:73,796
Bookmarked:23 times
Announcements
CompMonthly Competition
Loading...
Articles
Desktop Development
Button Controls
Clipboard
Combo & List Boxes
Dialogs and Windows
Desktop Gadgets
Document / View
Edit Controls
Files and Folders
Grid & Data Controls
List Controls
Menus
Miscellaneous
Printing
Progress Controls
Selection Controls
Shell and IE programming
Smart Client
Splitter Windows
Static & Panel Controls
Status Bar
Tabs & Property Pages
Toolbars & Docking windows
Tree Controls
Web Development
Ajax and Atlas
Applications & Tools
ASP
ASP.NET
ASP.NET Controls
ATL Server
Caching
Charts, Graphs and Images
Client side scripting
Custom Controls
HTML / CSS
ISAPI
Site & Server Management
Session State
Silverlight
Trace and Logs
User Controls
Validation
View State
WAP / WML
Web Security
Web Services
Enterprise Systems
Content Management Server
Microsoft BizTalk Server
Microsoft Exchange
Office Development
SharePoint Server
Multimedia
Audio and Video
DirectX
GDI
GDI+
General Graphics
OpenGL
Database
Database
SQL Reporting Services
Platforms, Frameworks & Libraries
ATL
MFC
STL
WTL
COM / COM+
.NET Framework
Win32/64 SDK & OS
Vista API
Vista Security
Cross Platform
Game Development
Mobile Development
Windows CardSpace
Windows Communication Foundation
Windows Presentation Foundation
Windows Workflow Foundation
Libraries
Windows Powershell
LINQ
Languages
C / C++ Language
C++ / CLI
C#
MSIL
VBScript
VB.NET
VB6 Interop
Other .NET Languages
XML
Java
General Programming
Algorithms & Recipes
Bugs & Workarounds
Collections
Cryptography & Security
Date and Time
DLLs & Assemblies
Exception Handling
Localisation
Macros and Add-ins
Programming Tips
String handling
Internet / Network
Threads, Processes & IPC
WinHelp / HTMLHelp
Uncategorised FAQ Entries
Graphics / Design
Expression
Usability
Development Lifecycle
Debug Tips
Design and Architecture
Installation
Work Issues
Testing and QA
Code Generation
General Reading
Book Chapters
Book Reviews
Hardware Reviews
Interviews
Scrapbook
Hardware & System
Uncategorised Technical Blogs
Author Resources
Third Party Products
Product Showcase
Solution Center
Mentor Resources
Services
Product Catalog
Code-signing Certificates
Job Board
CodeProject VS2008 Addin
Feature Zones
Product Showcase
 
Search    
Advanced Search
printPrint  addShare
     DiscussDiscuss  Broken Article?Report  
10 votes for this article.
Popularity: 2.21Rating:2.21 out of 5
3 votes, 30.0%
1
2 votes, 20.0%
2
3 votes, 30.0%
3
2 votes, 20.0%
4

5

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

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

A brief description about the component members:

  1. TheInstanceControl(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.
  2. TheIsAnyInstanceExist() 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"); }}

OUTPUT:

D:\vstudio>InstClientNo Instance runningAlreading 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 foundhere

About the Author

Chandra Hundigam


Member
Chandra Hundigam has Master degree in Computer Application, Microsoft Certified Professional and Software Architect. He's significantly involved in enterprise application development and distributed object oriented system development using Microsoft .Net, Sun Java/J2EE technology to serve global giants in the Media, Finance, Mortgage and Software Industries.Presently working as Software Consultant for a US-based company.His areas of interests are in emerging Technologies.
Occupation:Architect
Location:United States United States

Other popular C / C++ Language articles:

Article Top
Sign Up to vote for this article
Your reason for this vote:
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
GeneralFirst instance not in the process list [modified]memberJohn.Jurisoo12:18 17 Sep '07  
GeneralNo client application code there!!membermbthe2nd13:43 27 Nov '05  
GeneralRe: No client application code there!!memberChandra Hundigam7:41 2 Dec '05  
GeneralProcess class bug in .NETmemberallenLynx11:56 8 Sep '05  
GeneralMultiple AppDomains in single processmembertstih23:00 15 Apr '03  
GeneralRe: Multiple AppDomains in single processmemberJohn Hills6:20 17 Apr '03  
GeneralMutexmemberAdam Turner19:29 15 Apr '03  
GeneralRe: Mutexmembertstih23:03 15 Apr '03  
GeneralRe: MutexmemberAlexander Werner6:14 17 Apr '03  
GeneralRe: MutexmemberSanju Burkule13:23 21 Apr '03  
GeneralRe: MutexmemberMach0057:24 2 Apr '04  
GeneralRe: MutexmemberRequiem Sollar13:25 31 Jul '08  
GeneralNice CodesussAnonymous11:07 15 Apr '03  
GeneralRe: Nice CodememberAK5:46 17 Apr '03  
Last Visit: 0:55 26 Sep '09     Last Update: 0:55 26 Sep '091

General General   News News   Question Question   Answer Answer   Joke Joke   Rant Rant   Admin 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


[8]ページ先頭

©2009-2025 Movatter.jp