如果程序是 运行,则杀死其他实例?

Kill other instances if program is running?

我试图做到这一点,如果我的程序的另一个实例是 运行,它应该关闭已经是 运行 的实例并启动新实例。我目前试过这个:

[STAThread]
static void Main()
{
    Mutex mutex = new System.Threading.Mutex(false, "supercooluniquemutex");
    try
    {
        if (mutex.WaitOne(0, false))
        {
            // Run the application
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new fMain());
        }
        else
        {
            foreach (Process proc in Process.GetProcesses())
            {
                if (proc.ProcessName.Equals(Process.GetCurrentProcess().ProcessName) && proc.Id != Process.GetCurrentProcess().Id)
                {
                    proc.Kill();
                    break;
                }
            }
            // Run the application
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new fMain());
        }
    }
    finally
    {
        if (mutex != null)
        {
            mutex.Close();
            mutex = null;
        }
    }
}

但出于某种原因,它不会杀死已经 运行 的实例,大部分时间它只会杀死自己,有时它什么都不做。

我该怎么做才能让它发挥作用?

编辑: 我知道通常的做法是显示应用程序已经 运行 的消息,但在这个应用程序中,它杀死它是至关重要的旧进程而不是显示消息。

首先杀掉上一个进程后需要再次等待mutex。当您这样做时,您将获得 AbandonedMutexException。有关该 please check this link 的详细信息,我假设在该异常后可以继续。

你可以试试。

Mutex mutex = new System.Threading.Mutex(false, "supercooluniquemutex");
        try
        {
            bool tryAgain = true;
            while (tryAgain)
            {
                bool result = false;
                try
                {
                    result = mutex.WaitOne(0, false);
                }
                catch (AbandonedMutexException ex)
                {
                    // No action required
                    result = true;
                }
                if (result)
                {
                    // Run the application
                    tryAgain = false;
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new fMain());
                }
                else
                {
                    foreach (Process proc in Process.GetProcesses())
                    {
                        if (proc.ProcessName.Equals(Process.GetCurrentProcess().ProcessName) && proc.Id != Process.GetCurrentProcess().Id)
                        {
                            proc.Kill();
                            break;
                        }
                    }
                    // Wait for process to close
                    Thread.Sleep(2000);
                }
            }
        }
        finally
        {
            if (mutex != null)
            {
                mutex.Close();
                mutex = null;
            }
        }

在那个例子中,如果我得到 AbandonedMutexException,我就获得了互斥体的所有权,可以继续。

此外,您使用本地 Mutex,另一个用户可以 运行 在另一个终端服务器会话下使用相同的应用程序。 MSDN says

On a server that is running Terminal Services, a named system mutex can have two levels of visibility. If its name begins with the prefix "Global\", the mutex is visible in all terminal server sessions. If its name begins with the prefix "Local\", the mutex is visible only in the terminal server session where it was created. In that case, a separate mutex with the same name can exist in each of the other terminal server sessions on the server. If you do not specify a prefix when you create a named mutex, it takes the prefix "Local\". Within a terminal server session, two mutexes whose names differ only by their prefixes are separate mutexes, and both are visible to all processes in the terminal server session. That is, the prefix names "Global\" and "Local\" describe the scope of the mutex name relative to terminal server sessions, not relative to processes.