Click here to Skip to main content
15,887,135 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.2K   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: 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 
Hi.
I wonder if you can help me in a side issue arising from your comment. I am trying to find out all the ways that process.MainModule.FileName can fail (apart from the documented exceptions NotSupportedException and PlatformNotSupportedException). This was the only reference I found to the possibilty of it throwing an InvalidOperationException. Can you tell me how you found out about this - whether it is documented, or if you discovered it by experience. I assume from your code that it occurs if the process has exited, and possibly under other circumstances. Any information would be helpful. Thanks.

Dave

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

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.