public static bool CheckApplicationAlreadyRunning(bool switchToAlreadyRunningProcess)
{
Process[] processes = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
// > 1 because if there is already a process running, then 2 instances (with this one) are present
if (processes.Length > 1)
{
if (switchToAlreadyRunningProcess)
{
IntPtr hwnd = processes[0].Id != Process.GetCurrentProcess().Id ? processes[0].MainWindowHandle : processes[1].MainWindowHandle;
if (!NativeMethods.IsWindowVisible(hwnd))
{
NativeMethods.ShowWindowAsync(hwnd, 1); // maximize window
}
NativeMethods.SwitchToThisWindow(hwnd, true);
}
return true;
}
return false;
}