Don't start new instance of program when already running

  • Thread starter Thread starter Trygve Lorentzen
  • Start date Start date
T

Trygve Lorentzen

Hi,

how can I make my exe file open the already running instance of the program
instead of starting up a new instance? I'm developing in C# with VS 2003 and
..NET 1.1

Regards,
Trygve Lorentzen
 
rough and ready copy and paste
but shuold help

declare somewhere in main form class

// main uses this to prevent more than one instance of the application
running at a time.
private static string _appGuid = "yourGUIDgoeshere";

[STAThread]
static void Main()
{
// use _appGUID to check instance of this app isn't running already
using(Mutex mutex = new Mutex(false, _appGuid))
{
if(!mutex.WaitOne(0, false))
{
MessageBox.Show(
String.Format(System.Globalization.CultureInfo.CurrentCulture, "An
instance of {0} is already running.", AppDomain.CurrentDomain.FriendlyName),
String.Format(System.Globalization.CultureInfo.CurrentCulture,
"Program Already Open"),
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
return;
}

Application.Run(new Form1(true));
}
}

HTH
sam
 
Back
Top