Don't start new instance of program when already running

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
 
S

Sam Martin

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
 

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