Linux上的单实例dotnetcore CLI应用程序 [英] Single instance dotnetcore cli app on linux

查看:193
本文介绍了Linux上的单实例dotnetcore CLI应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何为dotnetcore控制台应用程序强制实施单实例策略感兴趣。令我惊讶的是,似乎该主题上没有太多内容。我发现了这个堆栈,如何将程序限制为一个实例,但在使用ubuntu的dotnetcore上对我来说似乎不起作用。

I am interested in how to inforce a single instance policy for dotnetcore console apps. To my surprise it seems like there isn't much out there on the topic. I found this one stacko, How to restrict a program to a single instance, but it doesnt seem to work for me on dotnetcore with ubuntu. Anyone here do this before?

推荐答案

@MusuNaji解决方案的不同之处在于:如何将程序限制为单个实例

Variation of @MusuNaji's solution at: 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;
    }

这篇关于Linux上的单实例dotnetcore CLI应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆