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

Posted

技术标签:

【中文标题】C# Windows 服务 - 我的计时器不工作,获得多个线程【英文标题】:C# Windows Service - My timer is not working, getting multiple threads 【发布时间】:2021-07-09 21:31:44 【问题描述】:

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

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

谢谢。

【问题讨论】:

【参考方案1】:

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

【讨论】:

谢谢,让计时器正常工作。 我仍然会收到太多线程,所以我仍在调查。

以上是关于C# Windows 服务 - 我的计时器不工作,获得多个线程的主要内容,如果未能解决你的问题,请参考以下文章

c# 在windows服务中 使用定时器

Windows 服务(带计时器)在没有控制台的情况下无法工作

c#计时器如何保存计时

C# Window Service 不调用定时器函数

计时器每秒触发一次并更新 GUI(C# Windows 窗体)

Windows 服务增加 CPU 消耗