Click here to Skip to main content
14,607,948 members

Single Instance Application in C#

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

Sample screenshot


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)
   //Write your program logic here


  • 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.
  • June 07. 2018 - Uploaded VS2015 project.


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


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.

Comments and Discussions

GeneralMy vote of 5 Pin
Sylwester Filaber14-Dec-17 6:28
MemberSylwester Filaber14-Dec-17 6:28 
GeneralRe: My vote of 5 Pin
Manish K. Agarwal10-Jan-18 19:15
MemberManish K. Agarwal10-Jan-18 19:15 
Questionerror msg Pin
Souleimane SEHAB2-Sep-16 4:51
MemberSouleimane SEHAB2-Sep-16 4:51 
QuestionI will try Pin
gemese16-Apr-16 2:12
Membergemese16-Apr-16 2:12 
QuestionRun a new Instance if application name is changed Pin
deo se multan25-Oct-15 22:05
Memberdeo se multan25-Oct-15 22:05 
AnswerRe: Run a new Instance if application name is changed Pin
Manish K. Agarwal5-Nov-15 21:55
MemberManish K. Agarwal5-Nov-15 21:55 
QuestionFor me the GetCurrentInstanceWindowHandle method is returning 0. Pin
Andrew Truckle16-Feb-15 10:52
professionalAndrew Truckle16-Feb-15 10:52 
AnswerRe: For me the GetCurrentInstanceWindowHandle method is returning 0. Pin
Andrew Truckle16-Feb-15 11:03
professionalAndrew Truckle16-Feb-15 11:03 
QuestionWorks straight out of the box! Pin
Bill Hardwick25-May-14 0:51
MemberBill Hardwick25-May-14 0:51 
AnswerRe: Works straight out of the box! Pin
Manish K. Agarwal26-May-14 22:18
MemberManish K. Agarwal26-May-14 22:18 
GeneralRe: Works straight out of the box! Pin
mla15422-Sep-14 7:13
Membermla15422-Sep-14 7:13 
AnswerThanks! Works nice. Pin
rurizarg21-Mar-14 8:15
Memberrurizarg21-Mar-14 8:15 
GeneralMy vote of 5 Pin
Fredrik Larsson11-Sep-13 22:30
MemberFredrik Larsson11-Sep-13 22:30 
GeneralMy vote of 5 Pin
YBP_RSA12-Aug-13 3:05
MemberYBP_RSA12-Aug-13 3:05 
GeneralMy vote of 5 Pin
pushkar7222-Jul-13 9:45
Memberpushkar7222-Jul-13 9:45 
GeneralMy vote of 5 Pin
Member 948757810-Apr-13 2:29
MemberMember 948757810-Apr-13 2:29 
BugUsing SingleApplication on Terminal Servers Pin
ErwinRichard7-Mar-13 0:37
MemberErwinRichard7-Mar-13 0:37 
QuestionPassing Command Line Arguments to the already running instance? Pin
vipul00926-Jan-13 17:29
Membervipul00926-Jan-13 17:29 
AnswerRe: Passing Command Line Arguments to the already running instance? Pin
Manish K. Agarwal1-Mar-13 0:50
MemberManish K. Agarwal1-Mar-13 0:50 
GeneralMy vote of 5 Pin
Yavarity3-Jan-13 21:42
MemberYavarity3-Jan-13 21:42 
GeneralMy vote of 5 Pin
Saumitra Kumar Paul7-Oct-12 6:06
MemberSaumitra Kumar Paul7-Oct-12 6:06 
Questionexcellent Pin
RajaM.Adnan25-Sep-12 1:06
MemberRajaM.Adnan25-Sep-12 1:06 
Questionthanks Pin
mx639229-May-12 15:31
Membermx639229-May-12 15:31 
GeneralThanks Pin
Cumulus723-Mar-12 0:20
MemberCumulus723-Mar-12 0:20 
GeneralMy vote of 5 Pin
Vikas K Solegaonkar9-Oct-11 15:30
professionalVikas K Solegaonkar9-Oct-11 15:30 

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.

Posted 28 Jun 2003


129 bookmarked