Credit: Chris Tacke
private const int ERROR_ALREADY_EXISTS = 183;
[DllImport("coredll.dll", EntryPoint="GetLastError")]
private static extern int GetLastError();
[DllImport("coredll.dll", EntryPoint="CreateMutexW")]
private static extern int CreateMutex(IntPtr lpMutexAttributes, bool InitialOwner, string MutexName);
public static bool IsInstanceRunning()
{
string appname = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
if(CreateMutex(IntPtr.Zero, true, appname) != 0)
{
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
DEBUGMSG(true, "Already running\r\n");
return true;
}
else
{
DEBUGMSG(true, "Not already running\r\n");
return false;
}
}
DEBUGMSG(true, "CreateMutex failed!\r\n");
return false;
}