Simple, yes... but not something I'd allow into production code. See
these links for some discussion.
Here's an alternative... first, a method to try to create a mutex unique to the application:
private Mutex GetSingleInstanceMutex()
{
bool createdNewMutex;
Mutex mutex = new Mutex(false, @"Global\" + SomethingUnique, out createdNewMutex);
if (!createdNewMutex)
{
mutex = null;
}
return mutex;
}
SomethingUnique should be something unique to the application, for example, the assembly GUID.
Here's how you'd use the method:
using (Mutex mutex = GetSingleInstanceMutex())
{
if (mutex != null)
{
// This is the only running instance; go ahead and run the application.
}
else
{
// Another instance is running; notify user and quit.
}
}