Limit C# Windows Application to single instance

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
 
G

Guest

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
 
C

cybertof

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

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top