Click here to Skip to main content
Click here to Skip to main content
Go to top

Single Instance Application in C#

, 12 Dec 2004
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.
Follow on   LinkedIn

Comments and Discussions

 
QuestionWorks straight out of the box! PinmemberBill Hardwick25-May-14 0:51 
AnswerRe: Works straight out of the box! PinmemberManish K. Agarwal26-May-14 22:18 
AnswerThanks! Works nice. Pinmemberrurizarg21-Mar-14 8:15 
GeneralMy vote of 5 PinmemberFredrik Larsson11-Sep-13 22:30 
GeneralMy vote of 5 PinmemberYPARSARD12-Aug-13 3:05 
GeneralMy vote of 5 Pinmemberpushkar7222-Jul-13 9:45 
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 

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 | Mobile
Web01 | 2.8.140916.1 | Last Updated 13 Dec 2004
Article Copyright 2003 by Manish K. Agarwal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid