Click here to Skip to main content
15,886,258 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 582K   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

 
AnswerRe: Minimized to System Tray Pin
booyakasha6-Feb-07 16:49
booyakasha6-Feb-07 16:49 
Generalproblem when packaged in a library Pin
peter wentworth4-Feb-07 2:34
peter wentworth4-Feb-07 2:34 
GeneralRe: problem when packaged in a library Pin
Manish K. Agarwal5-Feb-07 16:14
Manish K. Agarwal5-Feb-07 16:14 
Generalthanks Pin
khaadem16-Nov-06 21:30
khaadem16-Nov-06 21:30 
QuestionSilly question, but... Pin
TMarkham11-Aug-06 10:45
TMarkham11-Aug-06 10:45 
AnswerRe: Silly question, but... Pin
Manish K. Agarwal16-Aug-06 20:37
Manish K. Agarwal16-Aug-06 20:37 
GeneralRe: Silly question, but... Pin
TMarkham3-Nov-06 10:39
TMarkham3-Nov-06 10:39 
GeneralRe: Silly question, but... Pin
Manish K. Agarwal5-Nov-06 16:33
Manish K. Agarwal5-Nov-06 16:33 
GeneralRe: Silly question, but... Pin
jack_claudine4-Feb-07 20:30
jack_claudine4-Feb-07 20:30 
GeneralRe: Silly question, but... Pin
Mamtha Ragupathy13-Aug-08 4:03
Mamtha Ragupathy13-Aug-08 4:03 
GeneralRe: Silly question, but... Pin
Member 444311412-Feb-09 1:32
Member 444311412-Feb-09 1:32 
GeneralRe: Silly question, but... Pin
Manish K. Agarwal12-Feb-09 17:10
Manish K. Agarwal12-Feb-09 17:10 
Questionpassing args to running instance ? Pin
iDnet2-Apr-06 8:45
iDnet2-Apr-06 8:45 
AnswerRe: passing args to running instance ? Pin
iDnet2-Apr-06 8:50
iDnet2-Apr-06 8:50 
GeneralRe: passing args to running instance ? Pin
dotmartin24-Sep-07 3:23
dotmartin24-Sep-07 3:23 
GeneralThank you! Pin
Ravi Bhavnani11-Feb-06 9:44
professionalRavi Bhavnani11-Feb-06 9:44 
GeneralMultiple Forms Pin
PHDENG8113-Dec-05 1:17
PHDENG8113-Dec-05 1:17 
GeneralRe: Multiple Forms Pin
Manish K. Agarwal13-Dec-05 16:11
Manish K. Agarwal13-Dec-05 16:11 
GeneralGlobal Pin
David M. Kean13-Dec-04 12:30
David M. Kean13-Dec-04 12:30 
GeneralRe: Global Pin
Manish K. Agarwal13-Dec-04 16:24
Manish K. Agarwal13-Dec-04 16:24 
GeneralRe: Global Pin
Dan Neely23-Mar-07 3:39
Dan Neely23-Mar-07 3:39 
GeneralRoom for improvement Pin
mav.northwind12-Dec-04 21:07
mav.northwind12-Dec-04 21:07 
GeneralRe: Room for improvement Pin
Anonymous4-Jul-05 10:33
Anonymous4-Jul-05 10:33 
GeneralRe: Room for improvement Pin
Anubhava Dimri5-Jun-09 19:42
Anubhava Dimri5-Jun-09 19:42 
GeneralRe: Room for improvement Pin
Manish K. Agarwal7-Jun-09 17:59
Manish K. Agarwal7-Jun-09 17:59 

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.