Click here to Skip to main content
12,626,517 members (34,796 online)
Click here to Skip to main content
Add your own
alternative version

Stats

421.5K views
15.2K downloads
124 bookmarked
Posted

Single Instance Application in C#

, 12 Dec 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
An article demonstrating how to run a single instance of an application and activate previous one if already running.

Sample screenshot

Introduction

Single-instance an application means enabling the application to recognize, at startup, if another running instance of itself is already running, In this case, the new application stops its execution. Generally, for a form based application, the new instance activates (brings the application window to foreground) the previous instance, if its already running.

Using the code

To make a single instance application, add file SingleApplication.cs in your project. It adds a new class SingleApplication defined in namespace SingleInstance and adds the following code for a form based application to your startup code:

static void Main() 
{
   SingleInstance.SingleApplication.Run(new FrmMain());
}

FrmMain is the main form class name. The Run method returns false if any other instance of the application is already running. For a console based application, call Run( ) without any parameter and check the return value, if it is true you can execute your application.

Using with console application:

static void Main() 
{
   if(SingleInstance.SingleApplication.Run() == false)
   {
      return;
   }
   //Write your program logic here
}

History

  • June 29, 2003 - Initial release of article.
  • July 1, 2003 - Mutex support added.
  • December 11, 2004 - Improved performance on the basis of user feedback, special thanks to Mach005.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Manish K. Agarwal
Software Developer (Senior) Oracle
India India
Working with Oracle. Using C/C++, VC++, MFC, STL, C#, Java etc. on various platform like Windows, Unix, Macintosh etc. from last 13+ years to convert various type of requirements into running software components. My core expertise is multithreaded desktop product and large scale enterprises software development.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Questionerror msg Pin
Souleimane SEHAB2-Sep-16 5:51
memberSouleimane SEHAB2-Sep-16 5:51 
QuestionI will try Pin
gemese16-Apr-16 3:12
membergemese16-Apr-16 3:12 
QuestionRun a new Instance if application name is changed Pin
deo se multan25-Oct-15 23:05
memberdeo se multan25-Oct-15 23:05 
AnswerRe: Run a new Instance if application name is changed Pin
Manish K. Agarwal5-Nov-15 22:55
memberManish K. Agarwal5-Nov-15 22:55 
QuestionFor me the GetCurrentInstanceWindowHandle method is returning 0. Pin
Andrew Truckle16-Feb-15 11:52
memberAndrew Truckle16-Feb-15 11:52 
AnswerRe: For me the GetCurrentInstanceWindowHandle method is returning 0. Pin
Andrew Truckle16-Feb-15 12:03
memberAndrew Truckle16-Feb-15 12:03 
QuestionWorks straight out of the box! Pin
Bill Hardwick25-May-14 1:51
memberBill Hardwick25-May-14 1:51 
AnswerRe: Works straight out of the box! Pin
Manish K. Agarwal26-May-14 23:18
memberManish K. Agarwal26-May-14 23:18 
GeneralRe: Works straight out of the box! Pin
mla15422-Sep-14 8:13
membermla15422-Sep-14 8:13 
AnswerThanks! Works nice. Pin
rurizarg21-Mar-14 9:15
memberrurizarg21-Mar-14 9:15 
GeneralMy vote of 5 Pin
Fredrik Larsson11-Sep-13 23:30
memberFredrik Larsson11-Sep-13 23:30 
GeneralMy vote of 5 Pin
YPARSARD12-Aug-13 4:05
memberYPARSARD12-Aug-13 4:05 
GeneralMy vote of 5 Pin
pushkar7222-Jul-13 10:45
memberpushkar7222-Jul-13 10:45 
GeneralMy vote of 5 Pin
Member 948757810-Apr-13 3:29
memberMember 948757810-Apr-13 3:29 
BugUsing SingleApplication on Terminal Servers Pin
ErwinRichard7-Mar-13 1:37
memberErwinRichard7-Mar-13 1:37 
QuestionPassing Command Line Arguments to the already running instance? Pin
vipul00926-Jan-13 18:29
membervipul00926-Jan-13 18:29 
AnswerRe: Passing Command Line Arguments to the already running instance? Pin
Manish K. Agarwal1-Mar-13 1:50
memberManish K. Agarwal1-Mar-13 1:50 
GeneralMy vote of 5 Pin
YavarSystem3-Jan-13 22:42
memberYavarSystem3-Jan-13 22:42 
GeneralMy vote of 5 Pin
Saumitra Kumar Paul7-Oct-12 7:06
memberSaumitra Kumar Paul7-Oct-12 7:06 
Questionexcellent Pin
RajaM.Adnan25-Sep-12 2:06
memberRajaM.Adnan25-Sep-12 2:06 
Questionthanks Pin
mx639229-May-12 16:31
membermx639229-May-12 16:31 
GeneralThanks Pin
Cumulus723-Mar-12 1:20
memberCumulus723-Mar-12 1:20 
GeneralMy vote of 5 Pin
solegaonkar9-Oct-11 16:30
membersolegaonkar9-Oct-11 16:30 
Questionit opens extra windows Pin
Member 368006817-Aug-11 0:38
memberMember 368006817-Aug-11 0:38 
AnswerRe: it opens extra windows Pin
Manish K. Agarwal17-Aug-11 18:29
memberManish K. Agarwal17-Aug-11 18:29 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161205.3 | Last Updated 13 Dec 2004
Article Copyright 2003 by Manish K. Agarwal
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid