Limit C# Windows Application to single instance

  • Thread starter Thread starter Richard
  • Start date Start date
R

Richard

Hi,

Is there an easy way to limit a .NET application to a
single running instance, ala like checking the
hPrevInstance in MFC? October 2003 MSDN only has old
samples that use named mutexes; is there anything more
slick in .NET??

--Richard
 
Hi,

I use the following method. When true is returned I exit immediately.

private bool PreviousInstance()
{
return ( System.Diagnostics.Process.GetProcessesByName(
System.Diagnostics.Process.GetCurrentProcess().ProcessName ).Length > 1 );
}

Marcello
 
Try this :


using System.Diagnostics;

static void Main(string[] cmdLine )
{
Process[] myProcess = Process.GetProcessesByName("YourAppName");

if ( myProcess.Length > 1 )
{
MessageBox.Show( "The application is already launched...");
}
else
{
// Application Execution...

}
}
 
Back
Top