c# windows 服务未触发经过的事件处理程序

c# windows service not firing elapsed event handler

我有一个 windows 服务 运行 2 个计时器,一个间隔 15 分钟,一个间隔 24 小时。 我添加了事件日志记录以检查经过的事件处理程序是否在 15 分钟时触发,但它似乎没有。任何人都可以看到这段代码有什么问题吗?

public partial class GTstaging : ServiceBase
    {
        System.Timers.Timer regularTimer = new System.Timers.Timer();
        System.Timers.Timer longTimer = new System.Timers.Timer();
        DateTime _scheduleTime;

        public staging()
        {
            InitializeComponent();
            _scheduleTime = DateTime.Today.AddDays(1).AddHours(Convert.ToDouble(ConfigurationManager.AppSettings["ScheduleTime_" + DateTime.Now.DayOfWeek.ToString()]));
        }

        protected override void OnStart(string[] args)
        {
            //** original - ConsoleApplication1.Program.DoProcessing();

            using (EventLog eventLog = new EventLog("Application"))
            {
                eventLog.Source = "Application";
                eventLog.WriteEntry("START, regular timer:" + ConfigurationManager.AppSettings["RegularTimer"], EventLogEntryType.Information, 101, 1);
            }

            this.regularTimer.Enabled = true;
            this.regularTimer.Interval = Convert.ToDouble(ConfigurationManager.AppSettings["RegularTimer"]);
            this.regularTimer.AutoReset = true;
            this.regularTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.DoRegular);

            this.longTimer.Enabled = true;
            this.longTimer.Interval = _scheduleTime.Subtract(DateTime.Now).TotalSeconds * 1000;
            this.longTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.DoLongRunning);

        }

        private void DoRegular(object sender, System.Timers.ElapsedEventArgs e)
        {
//do stuff then log
                using (EventLog eventLog = new EventLog("Application"))
                {
                    eventLog.Source = "Application";
                    eventLog.WriteEntry("Regular Process End", EventLogEntryType.Information, 101, 1);
                }
        }

        private void DoLongRunning(object sender, System.Timers.ElapsedEventArgs e)
        {
            //do stuff
        }

        protected override void OnStop()
        {
            this.regularTimer.Stop();
            this.longTimer.Stop();
            this.regularTimer = null;
            this.longTimer = null;
        }

    }
}

我会这样调试:

  1. 在下一行放置一个断点,只是为了确保定时器是 设置,然后 运行 调试模式下的应用程序:

        this.longTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.DoLongRunning);
    
  2. 如果到达上一个断点,将定时器间隔设置为 5 和 10 秒,只是为了测试它们是否正常工作,然后 运行 中的应用程序 调试模式:

        this.regularTimer.Interval = 5000; //Convert.ToDouble(ConfigurationManager.AppSettings["RegularTimer"]);
    
        this.longTimer.Interval = 10000; //_scheduleTime.Subtract(DateTime.Now).TotalSeconds * 1000;
    

    然后在 DoRegular 方法中放置一个断点,在 DoLongRunning 方法中放置另一个断点,在调试模式下 运行 应用程序

  3. 如果您注意到 DoRegular 方法在几秒钟后到达 OnStartMethod 并且您看到达到了 DoLongRunning 方法 同样,我们只需要检查 this.regularTimer.Intervalthis.longTimer.Interval 已正确设置为您的原始来源 代码。

    你可以在 this.longTimer.Elapsed BreakPoint 时检查它们的值 是到达。 (记住该值以毫秒表示)

希望您通过这种方式修复代码,祝您有愉快的一天!