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;
}
}
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.