【问题标题】:c# windows service not firing elapsed event handler时间:2019-05-10 标签:c#windows service不触发elapsed事件处理程序
【发布时间】:2016-05-25 14:31:52
【问题描述】:

我有一个运行 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;
        }

    }
}

【问题讨论】:

  • ConfigurationManager.AppSettings["RegularTimer"]写入eventLog时的设置是什么?
  • 我想知道您是否尝试写入 DoRegular 事件处理程序中的文件而不是事件日志。我认为您正在两个不同的线程(服务的主线程和计时器事件处理程序)中访问日志。因此,您的事件很可能正在触发,但由于日志记录,您正在查看一个误报。 Check this MSDN page

标签: c# timer windows-services


【解决方案1】:

我会这样调试:

  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 时可以查看它们的值 是到达。 (记住值以毫秒为单位)

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

【讨论】:

    猜你喜欢
    • 2010-11-12
    • 1970-01-01
    • 2020-06-15
    • 2020-10-01
    • 2015-06-14
    • 2011-07-08
    • 2012-02-03
    • 2017-04-15
    • 2021-07-31
    相关资源
    最近更新 更多