Click here to Skip to main content
15,908,455 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 587.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

 
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 
Why should the notifyicon be left hanging around anyway? Lets consider this. In a normal application, it constructs your form and runs. What happens when the app terminates? The icon must be cleaned up, else the next time you run the app you will have a second icon and so on. So the fact that there is a singleton application class now should make no difference what-so-ever. The form should be modified to clean up it's resources on finalization IMO. Not the SingleApplication class. One reason for not passing the form type as a parameter and constructing it inside the SingleApplication class is because what if you want to pass parameters to your constructor. This becomes messy and in terms of ownership, the SingleApplication should not care how the form is created or destroyed. Further, what if you want several lines of code in the "construction" of your form. For instance to set certain properties etc. before it is shown by the Application.Run(); It may seem like a good idea to pass the type to the class but if you think about it does it really belong there? My answer is no.



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 
GeneralProcess Name Pin
Michael Potter30-Jun-03 3:29
Michael Potter30-Jun-03 3:29 
GeneralFYI Pin
Marc Clifton29-Jun-03 13:50
mvaMarc Clifton29-Jun-03 13:50 
GeneralWell done Pin
Dennis Battenfeld29-Jun-03 8:01
Dennis Battenfeld29-Jun-03 8:01 
GeneralRe: Well done Pin
Manish K. Agarwal29-Jun-03 17:37
Manish K. Agarwal29-Jun-03 17:37 
GeneralRating Pin
Kant29-Jun-03 5:50
Kant29-Jun-03 5:50 
GeneralNice !! Pin
Anonymous29-Jun-03 1:52
Anonymous29-Jun-03 1:52 

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.