最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# Start app from Windows service, only if it is not already running - Stack Overflow

programmeradmin4浏览0评论

Main application name is SysDemConnect. This application is started from desktop shortcut. When application is running, user can press button Install Windows Service.

Windows service is to start SysDemConnect on system restart so in case of loosing power or any unexpected shutdown, service will start and will start SysDem Connect. This works as expected.

Service is set to auto start after installation and this is expected/required behavior.

Issue: After installing a service, the service starts, but is also start another instance of SysDemConnect application.

Expected: When user have SysDemConnect opened and pressing Install Service. Service is installed but because there is already instance of SysDemConnect running, Service should not start new instance of SysDem Connect.

In the service in the OnStart() method I have below.

        protected override void OnStart(string[] args)
        {
            try
            {
                Process cmd = new Process();
                string filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SysDem.Connect.exe");
                cmd.StartInfo.FileName = filename;
                cmd.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                cmd.StartInfo.Arguments = null;
                cmd.Start();
                processId = cmd.Id;
                sessionID = cmd.SessionId;
            }
            catch (Exception ex)
            {
                ConnectServiceLogger.WriteToConnectServiceLog(ex.Message);
            }
        }

I already tried below but it looks like the OnStart() method is not executing when restarting windows, still I can see service running.

        protected override void OnStart(string[] args)
        {
            try
            {
                Process[] sysdemConnectProcess = Process.GetProcessesByName("SysDem.Connect");
                bool isRunning = sysdemConnectProcess.Length > 0;

                if (!isRunning)
                {
                    Process cmd = new Process();
                    string filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SysDem.Connect.exe");
                    cmd.StartInfo.FileName = filename;
                    cmd.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                    cmd.StartInfo.Arguments = null;
                    cmd.Start();
                    processId = cmd.Id;
                    sessionID = cmd.SessionId;
                }
            }
            catch (Exception ex)
            {
                ConnectServiceLogger.WriteToConnectServiceLog(ex.Message);
            }
        }

Any ideas how can I approach this issue?

Main application name is SysDemConnect. This application is started from desktop shortcut. When application is running, user can press button Install Windows Service.

Windows service is to start SysDemConnect on system restart so in case of loosing power or any unexpected shutdown, service will start and will start SysDem Connect. This works as expected.

Service is set to auto start after installation and this is expected/required behavior.

Issue: After installing a service, the service starts, but is also start another instance of SysDemConnect application.

Expected: When user have SysDemConnect opened and pressing Install Service. Service is installed but because there is already instance of SysDemConnect running, Service should not start new instance of SysDem Connect.

In the service in the OnStart() method I have below.

        protected override void OnStart(string[] args)
        {
            try
            {
                Process cmd = new Process();
                string filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SysDem.Connect.exe");
                cmd.StartInfo.FileName = filename;
                cmd.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                cmd.StartInfo.Arguments = null;
                cmd.Start();
                processId = cmd.Id;
                sessionID = cmd.SessionId;
            }
            catch (Exception ex)
            {
                ConnectServiceLogger.WriteToConnectServiceLog(ex.Message);
            }
        }

I already tried below but it looks like the OnStart() method is not executing when restarting windows, still I can see service running.

        protected override void OnStart(string[] args)
        {
            try
            {
                Process[] sysdemConnectProcess = Process.GetProcessesByName("SysDem.Connect");
                bool isRunning = sysdemConnectProcess.Length > 0;

                if (!isRunning)
                {
                    Process cmd = new Process();
                    string filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SysDem.Connect.exe");
                    cmd.StartInfo.FileName = filename;
                    cmd.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                    cmd.StartInfo.Arguments = null;
                    cmd.Start();
                    processId = cmd.Id;
                    sessionID = cmd.SessionId;
                }
            }
            catch (Exception ex)
            {
                ConnectServiceLogger.WriteToConnectServiceLog(ex.Message);
            }
        }

Any ideas how can I approach this issue?

Share Improve this question asked Mar 11 at 9:08 Lech KrawczykLech Krawczyk 214 bronze badges 8
  • Use some named event in your app. And not start it from service, if this event exist – RbMm Commented Mar 11 at 9:12
  • 1 There are several ways of solving this issue, but first: If the application is running and the user clicks the desktop shortcut, do you want to create another instance of the application, or not? I mean, should it always be a single instance application, or only when started from the service? – Zohar Peled Commented Mar 11 at 9:23
  • @RbMm I already have a timer created by the StartMethod() it runs every min and checks if sysdemconnect is running, if not it will start it again. So I could remove the start sysdemconnect from start method completely and let timer check. But idea was to keep as much of the original code as possible. – Lech Krawczyk Commented Mar 11 at 9:28
  • 2 I think that will only see other apps run by the same user. Consider an alternative strategy? – flackoverstow Commented Mar 11 at 11:17
  • 1 Consider re-writing that part using Mutex, like shown in this article – Zohar Peled Commented Mar 11 at 11:36
 |  Show 3 more comments

1 Answer 1

Reset to default 0

Everyone thanks for all your answers. After few more tests I managed to get it worked as expected. I created new class and placed the method inside that class.

    internal static class StartConnectWithService
    {
        internal static void StartConnectWithConnectService()
        {
            int processId;
            int sessionID;

            Process[] sysdemConnectProcessArray = Process.GetProcessesByName("SysDem.Connect");
            bool isRunning = sysdemConnectProcessArray.Length > 0;
            if (isRunning == false)
            {
                Process sysDemConnect = new Process();
                string filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SysDem.Connect.exe");
                sysDemConnect.StartInfo.FileName = filename;
                sysDemConnect.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                sysDemConnect.StartInfo.Arguments = null;
                sysDemConnect.Start();
                processId = sysDemConnect.Id;
                sessionID = sysDemConnect.SessionId;

                string messageToLog = $"=== SysDem Connect === \r" +
                    $"SysDem Connect Service has started SysDem Connect with \r" +
                    $"Process ID: {processId} \r" +
                    $"Session ID: {sessionID}";
                ConnectServiceLogger.WriteToConnectServiceLog(messageToLog);
            }
        }
    }

Then in OnStartMethod i call that method from another class.

        protected override void OnStart(string[] args)
        {
            try
            {
                CreateTimerOnServicestart();
                LogServiceStartStopToFile.LogServiceStart();
                StartConnectWithService.StartConnectWithConnectService();
            }
            catch (Exception ex)
            {
                ConnectServiceLogger.WriteToConnectServiceLog($"Exception: {ex.Message}");
            }
        }

Thanks again for all your help.

发布评论

评论列表(0)

  1. 暂无评论