Click here to Skip to main content
6,585,487 members and growing! (27,689 online)
Email Password   helpLost your password?
Languages » C# » How To     Intermediate License: The Code Project Open License (CPOL)

Single Instance Application in C#

By Manish K. Agarwal

An article demonstrating how to run a single instance of an application and activate previous one if already running.
C#.NET 1.1, Win2K, WinXPVS.NET2003, Dev
Posted:28 Jun 2003
Updated:12 Dec 2004
Views:155,078
Bookmarked:80 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
80 votes for this article.
Popularity: 7.57 Rating: 3.98 out of 5
13 votes, 16.5%
1
3 votes, 3.8%
2
2 votes, 2.5%
3
9 votes, 11.4%
4
52 votes, 65.8%
5

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


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
Occupation: Team Leader
Company: Nagarro
Location: India India

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 83 (Total in Forum: 83) (Refresh)FirstPrevNext
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  
GeneralThanks, this worked great for me! Pinmembersoundchaser5913:22 29 Sep '09  
General"GetCurrentInstanceWindowHandle" does not work Pinmemberazbe18:09 7 Jul '08  
GeneralRe: "GetCurrentInstanceWindowHandle" does not work PinmemberManish K. Agarwal20:02 7 Jul '08  
GeneralRe: "GetCurrentInstanceWindowHandle" does not work Pinmemberazbe15:53 8 Jul '08  
GeneralRe: "GetCurrentInstanceWindowHandle" does not work PinmemberManish K. Agarwal19:11 8 Jul '08  
GeneralRe: "GetCurrentInstanceWindowHandle" does not work Pinmemberazbe18:53 9 Jul '08  
GeneralRe: "GetCurrentInstanceWindowHandle" does not work PinmemberManish K. Agarwal19:54 9 Jul '08  
Questionpassing arguments Pinmembersuryahg21:24 2 Jul '08  
AnswerRe: passing arguments PinmemberManish K. Agarwal19:38 7 Jul '08  
GeneralRe: passing arguments Pinmembersuryahg19:59 7 Jul '08  
GeneralRe: passing arguments PinmemberManish K. Agarwal20:27 7 Jul '08  
GeneralThanks PinmemberSenthil S19:44 2 Jun '08  
GeneralThanks PinmemberSKP2420:55 18 Nov '07  
Generalthanks PinmemberQurex1:44 19 Oct '07  
GeneralMulti User problem Pinmemberehab hosny3:31 1 Oct '07  
GeneralRe: Multi User problem PinmemberManish K. Agarwal19:45 7 Jul '08  
GeneralRe: Multi User problem PinmemberJan Fiala7:22 3 Apr '09  
GeneralThanks! PinmemberThiago Falco5:10 23 Apr '07  
QuestionMinimized to System Tray Pinmemberbooyakasha19:08 4 Feb '07  
AnswerRe: Minimized to System Tray Pinmemberjack_claudine21:25 4 Feb '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Dec 2004
Editor: Smitha Vijayan
Copyright 2003 by Manish K. Agarwal
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project