Click here to Skip to main content
15,867,308 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 580.2K   19.9K   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: Thank you Pin
Manish K. Agarwal17-Aug-11 17:30
Manish K. Agarwal17-Aug-11 17:30 
Generalthanks a lot Pin
phucnth4-Nov-10 20:58
phucnth4-Nov-10 20:58 
GeneralApplication crashes Pin
Rohit Dixit7-Jun-10 2:26
Rohit Dixit7-Jun-10 2:26 
GeneralRe: Application crashes Pin
Manish K. Agarwal8-Jun-10 1:01
Manish K. Agarwal8-Jun-10 1:01 
GeneralA big problem with this program... Pin
GPUToaster™25-May-10 23:13
GPUToaster™25-May-10 23:13 
GeneralRe: A big problem with this program... Pin
Manish K. Agarwal26-May-10 20:00
Manish K. Agarwal26-May-10 20:00 
GeneralThanks for this great code... Pin
GPUToaster™27-May-10 0:08
GPUToaster™27-May-10 0:08 
GeneralWindows CE Pin
chrisclarke1123-May-10 22:13
chrisclarke1123-May-10 22:13 
Thanks for that been a great help.

Have you any idea how to apply this to a Windows CE program
Chris

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 
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 

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.