Click here to Skip to main content
Click here to Skip to main content

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


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.


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.
Follow on   LinkedIn

Comments and Discussions

QuestionWorks straight out of the box! PinmemberBill Hardwick25-May-14 1:51 
AnswerRe: Works straight out of the box! PinmemberManish K. Agarwal26-May-14 23:18 
GeneralRe: Works straight out of the box! Pinmembermla15422-Sep-14 8:13 
AnswerThanks! Works nice. Pinmemberrurizarg21-Mar-14 9:15 
GeneralMy vote of 5 PinmemberFredrik Larsson11-Sep-13 23:30 
GeneralMy vote of 5 PinmemberYPARSARD12-Aug-13 4:05 
GeneralMy vote of 5 Pinmemberpushkar7222-Jul-13 10:45 
GeneralMy vote of 5 PinmemberMember 948757810-Apr-13 3:29 
BugUsing SingleApplication on Terminal Servers [modified] PinmemberErwinRichard7-Mar-13 1:37 
QuestionPassing Command Line Arguments to the already running instance? Pinmembervipul00926-Jan-13 18:29 
AnswerRe: Passing Command Line Arguments to the already running instance? PinmemberManish K. Agarwal1-Mar-13 1:50 
GeneralMy vote of 5 PinmemberYavarSystem3-Jan-13 22:42 
GeneralMy vote of 5 PinmemberSaumitra Kumar Paul7-Oct-12 7:06 
Questionexcellent PinmemberRajaM.Adnan25-Sep-12 2:06 
Questionthanks Pinmembermx639229-May-12 16:31 
GeneralThanks PinmemberCumulus723-Mar-12 1:20 
GeneralMy vote of 5 Pinmembersolegaonkar9-Oct-11 16:30 
Questionit opens extra windows PinmemberMember 368006817-Aug-11 0:38 
AnswerRe: it opens extra windows PinmemberManish K. Agarwal17-Aug-11 18:29 
QuestionThank you PinmemberAlphein31-Jul-11 21:54 
AnswerRe: Thank you PinmemberManish K. Agarwal17-Aug-11 18:30 
Generalthanks a lot Pinmemberphucnth4-Nov-10 21:58 
GeneralApplication crashes PinmemberMember 37199507-Jun-10 3:26 
GeneralRe: Application crashes PinmemberManish K. Agarwal8-Jun-10 2:01 
GeneralA big problem with this program... PinmemberGPUToaster26-May-10 0:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web03 | 2.8.141216.1 | Last Updated 13 Dec 2004
Article Copyright 2003 by Manish K. Agarwal
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid