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

 
GeneralWindows CE Pin
chrisclarke1123-May-10 22:13
chrisclarke1123-May-10 22:13 
GeneralRe: Windows CE Pin
Manish K. Agarwal24-May-10 20:14
Manish K. Agarwal24-May-10 20:14 
GeneralThanks Pin
prasad.thunga5-May-10 20:38
prasad.thunga5-May-10 20:38 
GeneralMy Solution for Single Instance Application Pin
Member 293394818-Feb-10 23:13
Member 293394818-Feb-10 23:13 
GeneralRe: My Solution for Single Instance Application Pin
Manish K. Agarwal22-Feb-10 23:58
Manish K. Agarwal22-Feb-10 23:58 
GeneralRe: My Solution for Single Instance Application Pin
Xmen Real 4-Mar-12 16:06
professional Xmen Real 4-Mar-12 16:06 
GeneralNice work! Pin
Gideon van der Linde2-Dec-09 21:11
Gideon van der Linde2-Dec-09 21:11 
GeneralRe: Nice work! Pin
MartinFister18-Apr-10 5:33
MartinFister18-Apr-10 5:33 
For sure, it's amazing how little code can be used to accomplish this. I had thought it would be much more complicated.

Martin of teva flip flops
QuestionHow will i use this if the tray icon is the one that opens the main form? Pin
murasaki517-Nov-09 8:43
murasaki517-Nov-09 8:43 
AnswerRe: How will i use this if the tray icon is the one that opens the main form? Pin
Manish K. Agarwal18-Nov-09 21:08
Manish K. Agarwal18-Nov-09 21:08 
Generalhidden form ! Pin
n4b1l_00724-Oct-09 19:23
n4b1l_00724-Oct-09 19:23 
GeneralRe: hidden form ! Pin
Manish K. Agarwal24-Oct-09 22:05
Manish K. Agarwal24-Oct-09 22:05 
GeneralRe: hidden form ! Pin
n4b1l_00727-Oct-09 18:32
n4b1l_00727-Oct-09 18:32 
GeneralRe: hidden form ! Pin
Manish K. Agarwal28-Oct-09 19:42
Manish K. Agarwal28-Oct-09 19:42 
GeneralRe: hidden form ! Pin
Robert Simandl3-Nov-09 3:43
Robert Simandl3-Nov-09 3:43 
GeneralRe: hidden form ! Pin
Manish K. Agarwal4-Nov-09 18:17
Manish K. Agarwal4-Nov-09 18:17 
GeneralThanks, this worked great for me! Pin
soundchaser5929-Sep-09 12:22
soundchaser5929-Sep-09 12:22 
General"GetCurrentInstanceWindowHandle" does not work Pin
azbe7-Jul-08 17:09
azbe7-Jul-08 17:09 
GeneralRe: "GetCurrentInstanceWindowHandle" does not work Pin
Manish K. Agarwal7-Jul-08 19:02
Manish K. Agarwal7-Jul-08 19:02 
GeneralRe: "GetCurrentInstanceWindowHandle" does not work Pin
azbe8-Jul-08 14:53
azbe8-Jul-08 14:53 
GeneralRe: "GetCurrentInstanceWindowHandle" does not work Pin
Manish K. Agarwal8-Jul-08 18:11
Manish K. Agarwal8-Jul-08 18:11 
GeneralRe: "GetCurrentInstanceWindowHandle" does not work Pin
azbe9-Jul-08 17:53
azbe9-Jul-08 17:53 
GeneralRe: "GetCurrentInstanceWindowHandle" does not work Pin
Manish K. Agarwal9-Jul-08 18:54
Manish K. Agarwal9-Jul-08 18:54 
Questionpassing arguments Pin
suryahg2-Jul-08 20:24
suryahg2-Jul-08 20:24 
AnswerRe: passing arguments Pin
Manish K. Agarwal7-Jul-08 18:38
Manish K. Agarwal7-Jul-08 18:38 

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.