c #windows服务没有触发已经过去的事件处理程序

时间:2016-05-25 14:31:53

标签: c# timer windows-services

我有一个运行2个计时器的Windows服务,一个是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 个答案:

答案 0 :(得分:1)

我会这样调试:

  1. 在下一行放置一个BreakPoint,以确保定时器是 设置,然后在调试模式下运行应用程序:

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

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

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

  3. 如果您注意到某个秒后达到了DoRegular方法 OnStartMethod,您看到达到了DoLongRunning方法 我们只需要检查this.regularTimer.Intervalthis.longTimer.Interval已使用原始来源正确设置 码。

    您可以在this.longTimer.Elapsed BreakPoint时检查其值 到达。 (请记住,该值表示为毫秒)

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