Appccelerate.Windows.ApplicationHelper.CheckApplicationAlreadyRunning C# (CSharp) Method

CheckApplicationAlreadyRunning() public static method

Checks whether another instance of the same application is already running.
public static CheckApplicationAlreadyRunning ( bool switchToAlreadyRunningProcess ) : bool
switchToAlreadyRunningProcess bool Whether the already running process is flashed and brought to front.
return bool
        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;
        }