5,665,355 members and growing! (14,814 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#Windows, .NET, .NET 1.1, Win2K, WinXPVS.NET2003, Visual Studio, Dev

Posted: 28 Jun 2003
Updated: 12 Dec 2004
Views: 119,866
Bookmarked: 65 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
74 votes for this Article.
Popularity: 7.29 Rating: 3.90 out of 5
13 votes, 17.8%
1
3 votes, 4.1%
2
2 votes, 2.7%
3
9 votes, 12.3%
4
46 votes, 63.0%
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


I am masters in Computer Sc. from DCSA of Kurukshetra University, MS Software from BITS Pilani, India. Working with Nagarro, Gurgaon (Haryana - India) as a Technical Manager. My programming experience includes C/C++, VC++, MFC, ATL, COM, C# on various platform like Windows, Unix, Macintosh etc.. I have 8.5 years software development experience.

My core expertise is in multithreaded desktop product development on Windows.

I was born in Delhi, India. I love nature and traveling.

Thats it Smile
Occupation: Team Leader
Company: Nagarro
Location: India India

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 69 (Total in Forum: 69) (Refresh)FirstPrevNext
General"GetCurrentInstanceWindowHandle" does not workmemberazbe18:09 7 Jul '08  
GeneralRe: "GetCurrentInstanceWindowHandle" does not workmemberManish K. Agarwal20:02 7 Jul '08  
GeneralRe: "GetCurrentInstanceWindowHandle" does not workmemberazbe15:53 8 Jul '08  
GeneralRe: "GetCurrentInstanceWindowHandle" does not workmemberManish K. Agarwal19:11 8 Jul '08  
GeneralRe: "GetCurrentInstanceWindowHandle" does not workmemberazbe18:53 9 Jul '08  
GeneralRe: "GetCurrentInstanceWindowHandle" does not workmemberManish K. Agarwal19:54 9 Jul '08  
Questionpassing argumentsmembersuryahg21:24 2 Jul '08  
AnswerRe: passing argumentsmemberManish K. Agarwal19:38 7 Jul '08  
GeneralRe: passing argumentsmembersuryahg19:59 7 Jul '08  
GeneralRe: passing argumentsmemberManish K. Agarwal20:27 7 Jul '08  
GeneralThanksmemberSenthil S19:44 2 Jun '08  
GeneralThanksmemberSKP2420:55 18 Nov '07  
GeneralthanksmemberQurex1:44 19 Oct '07  
GeneralMulti User problemmemberehab hosny3:31 1 Oct '07  
GeneralRe: Multi User problemmemberManish K. Agarwal19:45 7 Jul '08  
GeneralThanks!memberThiago Falco5:10 23 Apr '07  
QuestionMinimized to System Traymemberbooyakasha19:08 4 Feb '07  
AnswerRe: Minimized to System Traymemberjack_claudine21:25 4 Feb '07  
GeneralRe: Minimized to System Traymemberbooyakasha15:30 5 Feb '07  
QuestionRe: Minimized to System Traymembermathulan7:46 9 Oct '08  
AnswerRe: Minimized to System Traymemberbooyakasha17:49 6 Feb '07  
Generalproblem when packaged in a librarymemberpeter wentworth3:34 4 Feb '07  
GeneralRe: problem when packaged in a librarymemberManish K. Agarwal17:14 5 Feb '07  
Generalthanksmemberkhaadem22:30 16 Nov '06  
QuestionSilly question, but...memberTMarkham11:45 11 Aug '06  

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-2008
Web20 | Advertise on the Code Project