Click here to Skip to main content
Licence CPOL
First Posted 28 Jun 2003
Views 271,221
Downloads 5,821
Bookmarked 98 times

Single Instance Application in C#

By Manish K. Agarwal | 12 Dec 2004
An article demonstrating how to run a single instance of an application and activate previous one if already running.
13 votes, 13.7%
1
4 votes, 4.2%
2
2 votes, 2.1%
3
12 votes, 12.6%
4
64 votes, 67.4%
5
4.66/5 - 96 votes
13 removed
μ 4.06, σa 2.54 [?]

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:

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:

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Manish K. Agarwal

Team Leader
Pitney Bowes
India India

Member
Working with Pitney Bowes Business Insight, Noida (India). Using C/C++, VC++, MFC, STL, C# etc. on various platform like Windows, Unix, Macintosh etc. from last 10 years to convert various type of requirements into running software components. My core expertise is multithreaded desktop product development for Windows.
 
Thats it Smile | :)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembersolegaonkar16:30 9 Oct '11  
Questionit opens extra windows PinmemberMember 36800680:38 17 Aug '11  
AnswerRe: it opens extra windows PinmemberManish K. Agarwal18:29 17 Aug '11  
QuestionThank you PinmemberAlphein21:54 31 Jul '11  
AnswerRe: Thank you PinmemberManish K. Agarwal18:30 17 Aug '11  
Generalthanks a lot Pinmemberphucnth21:58 4 Nov '10  
GeneralApplication crashes PinmemberMember 37199503:26 7 Jun '10  
GeneralRe: Application crashes PinmemberManish K. Agarwal2:01 8 Jun '10  
GeneralA big problem with this program... PinmemberGPUToaster0:13 26 May '10  
GeneralRe: A big problem with this program... PinmemberManish K. Agarwal21:00 26 May '10  
GeneralThanks for this great code... PinmemberGPUToaster1:08 27 May '10  
GeneralWindows CE Pinmemberchrisclarke1123:13 23 May '10  
GeneralRe: Windows CE PinmemberManish K. Agarwal21:14 24 May '10  
GeneralThanks Pinmemberprasad.thunga21:38 5 May '10  
GeneralMy Solution for Single Instance Application PinmemberMember 29339480:13 19 Feb '10  
GeneralRe: My Solution for Single Instance Application PinmemberManish K. Agarwal0:58 23 Feb '10  
GeneralNice work! PinmemberGideon van der Linde22:11 2 Dec '09  
GeneralRe: Nice work! PinmemberMartinFister6:33 18 Apr '10  
QuestionHow will i use this if the tray icon is the one that opens the main form? Pinmembermurasaki59:43 17 Nov '09  
AnswerRe: How will i use this if the tray icon is the one that opens the main form? PinmemberManish K. Agarwal22:08 18 Nov '09  
Generalhidden form ! Pinmembern4b1l_00720:23 24 Oct '09  
GeneralRe: hidden form ! PinmemberManish K. Agarwal23:05 24 Oct '09  
GeneralRe: hidden form ! Pinmembern4b1l_00719:32 27 Oct '09  
GeneralRe: hidden form ! PinmemberManish K. Agarwal20:42 28 Oct '09  
GeneralRe: hidden form ! PinmemberRobert Simandl4:43 3 Nov '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120209.1 | Last Updated 13 Dec 2004
Article Copyright 2003 by Manish K. Agarwal
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid