【问题标题】:VS 2015 C# - ElapsedEventHandler not firing in ServiceVS 2015 C# - ElapsedEventHandler 未在服务中触发
【发布时间】:2017-12-14 18:22:59
【问题描述】:

我在 VS 2015 中开发了我的第一个 C# 服务,但我无法触发我的 ElapsedEventHandler 方法。我有以下代码:

using System;
using System.ServiceProcess;
using System.Timers;

namespace UpdateEnvironmentService
{
    public partial class Scheduler : ServiceBase
    {
        private Timer timer = null;
        public Scheduler()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            timer = new Timer();
            this.timer.Interval = Convert.ToDouble(1000); //timer intraval in milliseconds
            this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.UpdateData);
            timer.Enabled = true;
            Library.WriteLog("Data Updater Started ");
        }

        private void UpdateData(object sender, EventArgs e)
        {
            Library.WriteLog("Got to update Data ");
        }

        protected override void OnStop()
        {
            timer.Enabled = false;
            timer = null;
            Library.WriteLog("Data Updater Stopped ");
        }
    }
}

Data Updater Started 行打印到我的日志文件中,但我从未看到 Got to update Data 甚至 Data Updater Stopped >。看来我的 ElapsedEventHandler 永远不会触发。有人知道为什么吗?

【问题讨论】:

    标签: c# visual-studio visual-studio-2015 service


    【解决方案1】:

    我建议您参考documentation on MSDNSystem.Timers.Timer 课程。

    计时器的大多数示例和用法倾向于避免直接设置Enabled,而是依赖StartStop 方法。

    顺便说一句,我建议在Task 时代,您以不同的方式解决问题:

    namespace UpdateEnvironmentService
    {
        public partial class Scheduler : ServiceBase
        {
            private readonly CancellationTokenSource _tcs;
            public Scheduler()
            {
                InitializeComponent();
                _tcs = new CancellationTokenSource();
            }
    
            protected override void OnStart(string[] args)
            {
                Library.WriteLog("Data Updater Started ");
                Task.Factory.StartNew(Runner, _tcs.Token);
            }
    
            private async void Runner()
            {
                Library.WriteLog("In runner");
                var delay = TimeSpan.FromSeconds(1);
                while(!_tcs.IsCancellationRequested)
                {
                    Library.WriteLog("Waiting...");
                    await Task.Delay(delay, _tcs.Token);
                    UpdateData();
                }
                Library.WriteLog("Cancellation requested; exiting runner");
            }
    
            private void UpdateData()
            {
                Library.WriteLog("Got to update Data ");
            }
    
            protected override void OnStop()
            {
                _tcs.Cancel();
                Library.WriteLog("Data Updater Stopped ");
            }
        }
    }
    

    这种方法不需要计时器,而是从任务中引入异步,允许线程池管理延迟;它还具有引入更好的取消控制的好处,这意味着它甚至可以在等待延迟时被取消!

    【讨论】:

      猜你喜欢
      • 2016-05-29
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      • 1970-01-01
      相关资源
      最近更新 更多