C# Windows 服务 - 我的计时器不工作,获取多个线程

问题描述

我编写了一个服务来检查应用程序是否正在运行,如果没有,它会启动它。计时器应该让服务每 30 秒运行一次。

我的代码工作不正常。它会在启动时启动多个线程,然后停止并且永远不会启动另一个实例,它只是坐在那里。

我需要它来启动一个线程,等待 30 秒,然后启动另一个

namespace ProcessRestart
{
    public partial class Service1 : ServiceBase
    {
        static string _exeName = ConfigurationManager.AppSettings["_exeName"].ToString();
        static string _exePath = ConfigurationManager.AppSettings["_exePath"].ToString();
        static string _logFilePath = ConfigurationManager.AppSettings["_logFilePath"].ToString();
        StringBuilder sb = new StringBuilder();
        Timer timer = new Timer();

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            sb.AppendLine(DateTime.Now + " Service started up.");
            System.IO.File.AppendAllText(_logFilePath,sb.ToString());

            CheckProcess();
            timer.Interval = 30000; // 30 seconds
            timer.Enabled = true;
            timer.Start();

            sb.AppendLine(DateTime.Now + " Timer set to " + timer.Interval);
            System.IO.File.AppendAllText(_logFilePath,sb.ToString());
        }

        protected override void OnStop()
        {
            timer.Stop();
            timer.dispose();
            timer = null;
        }

        private void Onelapsedtime(object source,ElapsedEventArgs e)
        {
            sb.AppendLine(DateTime.Now + " Service recalled.");
            System.IO.File.AppendAllText(_logFilePath,sb.ToString());
        }

        private void CheckProcess()
        {
            Process[] processName = Process.GetProcessesByName(_exeName);

            sb.AppendLine(DateTime.Now + " Number of instances of " + _exeName + " running:" + processName.Length);
            System.IO.File.AppendAllText(_logFilePath,sb.ToString());

            if (processName.Length <= 0) // Process is not running,we need to start it up
            {
                sb.AppendLine(DateTime.Now + " " + _exeName + " is not running,so starting it up Now.");
                System.IO.File.AppendAllText(_logFilePath,sb.ToString());
                Process.Start(_exePath);
            }
            else
            {
                sb.AppendLine(DateTime.Now + " " + _exeName + " is already running.");
                System.IO.File.AppendAllText(_logFilePath,sb.ToString());
            }
        }
    }
}

日志如下所示:

4/14/2021 2:52:42 PM Service started up.
4/14/2021 2:52:42 PM Service started up.
4/14/2021 2:52:42 PM Number of instances of process running: 1
4/14/2021 2:52:42 PM Service started up.
4/14/2021 2:52:42 PM Number of instances of process running: 1
4/14/2021 2:52:42 PM Process is already running.
4/14/2021 2:52:42 PM Service started up.
4/14/2021 2:52:42 PM Number of instances of process running: 1
4/14/2021 2:52:42 PM Process is already running.
4/14/2021 2:52:42 PM Timer set to 30000

有人知道我做错了什么吗?

谢谢。

解决方法

CheckProcess(); 应该在 OnElapsedTime 中而不是在 OnStart 中调用。