【问题标题】:Why won't my timer event kickoff?为什么我的计时器事件不会启动?
【发布时间】:2014-05-27 19:56:00
【问题描述】:

我有一个带有类级别计时器的 Windows 服务。 Elapsed 事件调用此 Work 方法:

protected override void Work()
    {
        if (!File.Exists(@"C:\Users\Will\Desktop\Developer stuff\junk text files\reusableservicetest.txt"))
            File.Create(@"C:\Users\Will\Desktop\Developer stuff\junk text files\reusableservicetest.txt");
        File.WriteAllLines(@"C:\Users\Will\Desktop\Developer stuff\junk text files\reusableservicetest.txt",
            new[] { DateTime.Now.ToLongTimeString() });
    }

如您所见,它只是将日期时间写入 txt 文件。该服务安装并启动正常,但不会写入日期时间。我有一段时间试图让这个 poc 工作。它应该是这里常用模式的抽象:每隔一段时间做一些工作。女士们或先生们,你们知道我哪里错了吗? 这是基类:

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

namespace WF.EMC.AVP.CommonCore.Utilities
{
public abstract class PollingServiceBase : ServiceBase
{
    private readonly Timer _timer;
    private System.ComponentModel.IContainer components = null;
    private string _serviceName;

    protected PollingServiceBase(int pollingMinutes, string serviceName)
    {
        _serviceName = serviceName;
        var pollingInterval = new TimeSpan(0, 0, pollingMinutes, 0);
        _timer = new Timer(pollingInterval.TotalMilliseconds) {AutoReset = true};
        _timer.Elapsed += timer_Elapsed;
    }

    void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        Work();
    }

     protected override void OnStart(string[] args)
    {
        _timer.Start();
    }

     protected override void OnStop()
     {
         _timer.Stop();
     }

        protected abstract void Work();

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }


        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        public void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.ServiceName = _serviceName;
        }

}
}

现在我们有了孩子:

namespace TestService7
{

public partial class Service1 : PollingServiceBase
{
    public Service1(int pollingMinutes = 1, string serviceName = "TestService7")
        : base(pollingMinutes, serviceName)
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
    }

    protected override void OnStop()
    {
    }

    protected override void Work()
    {
        if (!File.Exists(@"C:\Users\Will\Desktop\Developer stuff\junk text files\reusableservicetest.txt"))
            File.Create(@"C:\Users\Will\Desktop\Developer stuff\junk text files\reusableservicetest.txt");
        File.WriteAllLines(@"C:\Users\Will\Desktop\Developer stuff\junk text files\reusableservicetest.txt",
            new[] { DateTime.Now.ToLongTimeString() });
    }
}
}

【问题讨论】:

  • 没有包含任何相关代码时出错了
  • 包括涉及你的计时器的代码。
  • 我不明白为什么Work 代码是相关的,问题在于调用该方法。展示你如何连接计时器事件。
  • 那里。对不起,大家!

标签: c# timer windows-services


【解决方案1】:

您使用空方法覆盖了 OnStart。所以你的计时器不会启动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 2018-02-15
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    相关资源
    最近更新 更多