Click here to Skip to main content
15,885,278 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.9K   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

 
GeneralRe: Room for improvement Pin
Anubhava Dimri7-Jun-09 18:14
Anubhava Dimri7-Jun-09 18:14 
GeneralRe: Room for improvement Pin
Manish K. Agarwal9-Jun-09 18:10
Manish K. Agarwal9-Jun-09 18:10 
GeneralRe: Room for improvement Pin
Anubhava Dimri9-Jun-09 18:30
Anubhava Dimri9-Jun-09 18:30 
GeneralSome comments Pin
Mach0052-Apr-04 1:33
Mach0052-Apr-04 1:33 
GeneralRe: Some comments Pin
Manish K. Agarwal2-Apr-04 3:24
Manish K. Agarwal2-Apr-04 3:24 
GeneralRe: Some comments Pin
Mach0054-Apr-04 23:12
Mach0054-Apr-04 23:12 
GeneralRe: Some comments Pin
szabelin10-May-04 7:45
szabelin10-May-04 7:45 
GeneralRe: Vast Improvement Pin
laduran16-Dec-05 6:56
laduran16-Dec-05 6:56 
GeneralRe: Vast Improvement Pin
Mach00517-Dec-05 20:55
Mach00517-Dec-05 20:55 
QuestionRe: Vast Improvement Pin
popoli1-Jan-06 21:51
popoli1-Jan-06 21:51 
QuestionRe: Vast Improvement Pin
JoeKau14-Mar-06 2:10
JoeKau14-Mar-06 2:10 
GeneralRe: Vast Improvement Pin
Dave Midgley1-Nov-06 22:01
Dave Midgley1-Nov-06 22:01 
QuestionRe: Some comments Pin
striker776-Jan-06 8:29
striker776-Jan-06 8:29 
GeneralAbsolutely Superb !!!!!! Pin
Maharishi Bhatia30-Jan-04 4:30
Maharishi Bhatia30-Jan-04 4:30 
Generalpb with notifyicon Pin
jcmag30-Jun-03 23:03
jcmag30-Jun-03 23:03 
GeneralRe: pb with notifyicon Pin
Manish K. Agarwal1-Jul-03 0:23
Manish K. Agarwal1-Jul-03 0:23 
GeneralRe: pb with notifyicon Pin
jcmag1-Jul-03 0:36
jcmag1-Jul-03 0:36 
GeneralRe: pb with notifyicon Pin
Manish K. Agarwal1-Jul-03 1:17
Manish K. Agarwal1-Jul-03 1:17 
GeneralRe: pb with notifyicon Pin
Frode N. Rosand12-Jul-03 16:39
Frode N. Rosand12-Jul-03 16:39 
GeneralRe: pb with notifyicon Pin
Manish K. Agarwal12-Jul-03 23:27
Manish K. Agarwal12-Jul-03 23:27 
GeneralRe: pb with notifyicon Pin
Mach0055-Apr-04 0:34
Mach0055-Apr-04 0:34 
GeneralRe: pb with notifyicon Pin
bschubarg30-Mar-06 9:08
bschubarg30-Mar-06 9:08 
GeneralI rated the article a 4, but... Pin
kbuchan30-Jun-03 4:38
kbuchan30-Jun-03 4:38 
GeneralRe: I rated the article a 4, but... Pin
Michael Potter30-Jun-03 6:08
Michael Potter30-Jun-03 6:08 
GeneralRe: I rated the article a 4, but... Pin
Manish K. Agarwal30-Jun-03 18:00
Manish K. Agarwal30-Jun-03 18:00 

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.