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

C# Windows Service - My timer is not working, getting multiple threads

我编写了一个服务来检查应用程序是否 运行,如果不是,它会启动它。计时器应该每 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 中调用。