我对如何为 dotnetcore 控制台应用程序实现单实例策略感兴趣。令我惊讶的是,该主题似乎没有太多内容。我找到了这个 stacko How to restrict a program to a single instance ,但它似乎对我在 dotnetcore 和 ubuntu 上不起作用。这里有人以前做过吗?

最佳答案

@MusuNaji 解决方案的变体:How to restrict a program to a single instance

    private static bool AlreadyRunning()
    {
        Process[] processes = Process.GetProcesses();
        Process currentProc = Process.GetCurrentProcess();
        logger.LogDebug("Current proccess: {0}", currentProc.ProcessName);
        foreach (Process process in processes)
        {
            if (currentProc.ProcessName == process.ProcessName && currentProc.Id != process.Id)
            {
                logger.LogInformation("Another instance of this process is already running: {pid}", process.Id);
                return true;
            }
        }
        return false;
    }

关于c# - Linux 上的单实例 dotnetcore cli 应用程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44575472/

10-10 15:25