Click here to Skip to main content
15,887,214 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.3K   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

 
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 
"GetCurrentInstanceWindowHandle()" does not work.Therefore "SwitchToCurrentInstance()" does not work.
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 
GeneralRe: passing arguments Pin
suryahg7-Jul-08 18:59
suryahg7-Jul-08 18:59 
GeneralRe: passing arguments Pin
Manish K. Agarwal7-Jul-08 19:27
Manish K. Agarwal7-Jul-08 19:27 
GeneralThanks Pin
Senthil S2-Jun-08 18:44
Senthil S2-Jun-08 18:44 
GeneralThanks Pin
SKP2418-Nov-07 19:55
SKP2418-Nov-07 19:55 
Generalthanks Pin
Qurex19-Oct-07 0:44
Qurex19-Oct-07 0:44 
GeneralMulti User problem Pin
ehab hosny1-Oct-07 2:31
ehab hosny1-Oct-07 2:31 
GeneralRe: Multi User problem Pin
Manish K. Agarwal7-Jul-08 18:45
Manish K. Agarwal7-Jul-08 18:45 
GeneralRe: Multi User problem Pin
Jan Fiala3-Apr-09 6:22
Jan Fiala3-Apr-09 6:22 
GeneralRe: Multi User problem Pin
Ronni Marker12-Nov-09 7:13
Ronni Marker12-Nov-09 7:13 
GeneralRe: Multi User problem Pin
Ronni Marker12-Nov-09 7:25
Ronni Marker12-Nov-09 7:25 

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.