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

Single Instance Application in C#

By , 12 Dec 2004
 

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)

About the Author

Manish K. Agarwal
Team Leader Pitney Bowes
India India
Working with Pitney Bowes, Noida (India). Using C/C++, VC++, MFC, STL, C#, Java etc. on various platform like Windows, Unix, Macintosh etc. from last 12 years to convert various type of requirements into running software components. My core expertise is multithreaded desktop product development on Windows.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5 PinmemberMember 948757810-Apr-13 2:29 
BugUsing SingleApplication on Terminal Servers [modified] PinmemberErwinRichard7-Mar-13 0:37 
QuestionPassing Command Line Arguments to the already running instance? Pinmembervipul00926-Jan-13 17:29 
AnswerRe: Passing Command Line Arguments to the already running instance? PinmemberManish K. Agarwal1-Mar-13 0:50 
GeneralMy vote of 5 PinmemberYavarSystem3-Jan-13 21:42 
GeneralMy vote of 5 PinmemberSaumitra Kumar Paul7-Oct-12 6:06 
Questionexcellent PinmemberRajaM.Adnan25-Sep-12 1:06 
Questionthanks Pinmembermx639229-May-12 15:31 
GeneralThanks PinmemberCumulus723-Mar-12 0:20 
GeneralMy vote of 5 Pinmembersolegaonkar9-Oct-11 15:30 
Questionit opens extra windows PinmemberMember 368006816-Aug-11 23:38 
AnswerRe: it opens extra windows PinmemberManish K. Agarwal17-Aug-11 17:29 
QuestionThank you PinmemberAlphein31-Jul-11 20:54 
AnswerRe: Thank you PinmemberManish K. Agarwal17-Aug-11 17:30 
Generalthanks a lot Pinmemberphucnth4-Nov-10 20:58 
GeneralApplication crashes PinmemberMember 37199507-Jun-10 2:26 
GeneralRe: Application crashes PinmemberManish K. Agarwal8-Jun-10 1:01 
GeneralA big problem with this program... PinmemberGPUToaster25-May-10 23:13 
GeneralRe: A big problem with this program... PinmemberManish K. Agarwal26-May-10 20:00 
GeneralThanks for this great code... PinmemberGPUToaster27-May-10 0:08 
GeneralWindows CE Pinmemberchrisclarke1123-May-10 22:13 
GeneralRe: Windows CE PinmemberManish K. Agarwal24-May-10 20:14 
GeneralThanks Pinmemberprasad.thunga5-May-10 20:38 
GeneralMy Solution for Single Instance Application PinmemberMember 293394818-Feb-10 23:13 
GeneralRe: My Solution for Single Instance Application PinmemberManish K. Agarwal22-Feb-10 23:58 
GeneralRe: My Solution for Single Instance Application PinmemberXmen W.K.4-Mar-12 16:06 
GeneralNice work! PinmemberGideon van der Linde2-Dec-09 21:11 
GeneralRe: Nice work! PinmemberMartinFister18-Apr-10 5:33 
QuestionHow will i use this if the tray icon is the one that opens the main form? Pinmembermurasaki517-Nov-09 8:43 
AnswerRe: How will i use this if the tray icon is the one that opens the main form? PinmemberManish K. Agarwal18-Nov-09 21:08 
Generalhidden form ! Pinmembern4b1l_00724-Oct-09 19:23 
GeneralRe: hidden form ! PinmemberManish K. Agarwal24-Oct-09 22:05 
GeneralRe: hidden form ! Pinmembern4b1l_00727-Oct-09 18:32 
GeneralRe: hidden form ! PinmemberManish K. Agarwal28-Oct-09 19:42 
GeneralRe: hidden form ! PinmemberRobert Simandl3-Nov-09 3:43 
GeneralRe: hidden form ! PinmemberManish K. Agarwal4-Nov-09 18:17 
GeneralThanks, this worked great for me! Pinmembersoundchaser5929-Sep-09 12:22 
General"GetCurrentInstanceWindowHandle" does not work Pinmemberazbe7-Jul-08 17:09 
GeneralRe: "GetCurrentInstanceWindowHandle" does not work PinmemberManish K. Agarwal7-Jul-08 19:02 
GeneralRe: "GetCurrentInstanceWindowHandle" does not work Pinmemberazbe8-Jul-08 14:53 
GeneralRe: "GetCurrentInstanceWindowHandle" does not work PinmemberManish K. Agarwal8-Jul-08 18:11 
GeneralRe: "GetCurrentInstanceWindowHandle" does not work Pinmemberazbe9-Jul-08 17:53 
GeneralRe: "GetCurrentInstanceWindowHandle" does not work PinmemberManish K. Agarwal9-Jul-08 18:54 
Questionpassing arguments Pinmembersuryahg2-Jul-08 20:24 
AnswerRe: passing arguments PinmemberManish K. Agarwal7-Jul-08 18:38 
GeneralRe: passing arguments Pinmembersuryahg7-Jul-08 18:59 
GeneralRe: passing arguments PinmemberManish K. Agarwal7-Jul-08 19:27 
GeneralThanks PinmemberSenthil S2-Jun-08 18:44 
GeneralThanks PinmemberSKP2418-Nov-07 19:55 
Generalthanks PinmemberQurex19-Oct-07 0:44 

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.

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