Click here to Skip to main content
15,880,608 members
Articles / Programming Languages / C# 3.5

Single Instance Application in C#

Rate me:
Please Sign up or sign in to vote.
4.87/5 (122 votes)
12 Dec 2004CPOL1 min read 581.3K   20K   131   131
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:

C#
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:

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

License

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


Written By
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
Sylwester Filaber14-Dec-17 6:28 
GeneralRe: My vote of 5 Pin
Manish K. Agarwal10-Jan-18 19:15
Manish K. Agarwal10-Jan-18 19:15 
Questionerror msg Pin
Souleimane SEHAB2-Sep-16 4:51
Souleimane SEHAB2-Sep-16 4:51 
QuestionI will try Pin
gemese16-Apr-16 2:12
gemese16-Apr-16 2:12 
Thank you so much.
QuestionRun a new Instance if application name is changed Pin
deo se multan25-Oct-15 22:05
deo se multan25-Oct-15 22:05 
AnswerRe: Run a new Instance if application name is changed Pin
Manish K. Agarwal5-Nov-15 21:55
Manish 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
Bill Hardwick25-May-14 0:51 
AnswerRe: Works straight out of the box! Pin
Manish K. Agarwal26-May-14 22:18
Manish K. Agarwal26-May-14 22:18 
GeneralRe: Works straight out of the box! Pin
mla15422-Sep-14 7:13
mla15422-Sep-14 7:13 
AnswerThanks! Works nice. Pin
rurizarg21-Mar-14 8:15
rurizarg21-Mar-14 8:15 
GeneralMy vote of 5 Pin
Fredrik Larsson11-Sep-13 22:30
Fredrik Larsson11-Sep-13 22:30 
GeneralMy vote of 5 Pin
YBP_RSA12-Aug-13 3:05
YBP_RSA12-Aug-13 3:05 
GeneralMy vote of 5 Pin
pushkar7222-Jul-13 9:45
pushkar7222-Jul-13 9:45 
GeneralMy vote of 5 Pin
Member 948757810-Apr-13 2:29
Member 948757810-Apr-13 2:29 
BugUsing SingleApplication on Terminal Servers Pin
ErwinRichard7-Mar-13 0:37
ErwinRichard7-Mar-13 0:37 
QuestionPassing Command Line Arguments to the already running instance? Pin
vipul00926-Jan-13 17:29
vipul00926-Jan-13 17:29 
AnswerRe: Passing Command Line Arguments to the already running instance? Pin
Manish K. Agarwal1-Mar-13 0:50
Manish K. Agarwal1-Mar-13 0:50 
GeneralMy vote of 5 Pin
Yavarity3-Jan-13 21:42
Yavarity3-Jan-13 21:42 
GeneralMy vote of 5 Pin
Saumitra Kumar Paul7-Oct-12 6:06
Saumitra Kumar Paul7-Oct-12 6:06 
Questionexcellent Pin
RajaM.Adnan25-Sep-12 1:06
RajaM.Adnan25-Sep-12 1:06 
Questionthanks Pin
mx639229-May-12 15:31
mx639229-May-12 15:31 
GeneralThanks Pin
Cumulus723-Mar-12 0:20
Cumulus723-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.