static class Program
{
// Mutex can be made static so that GC doesn't recycle
// same effect with GC.KeepAlive(mutex) at the end of main
static Mutex mutex = new Mutex(false, "some-unique-id");
[STAThread]
static void Main()
{
// if you like to wait a few seconds in case that the instance is just
// shutting down
if (!mutex.WaitOne(TimeSpan.FromSeconds(2), false))
{
MessageBox.Show("Application already started!", "", MessageBoxButtons.OK);
return;
}
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
finally { mutex.ReleaseMutex(); } // I find this more explicit
}
}
One note about the some-unique-id -> this should be unique on the machine, so use something like your company name / application name.
Edit:
http://sanity-free.org/143/csharp_dotnet_single_instance_application.html