Win服务中的计时器不调用运行事件

本文关键字:调用 运行 事件 计时器 服务 Win | 更新日期: 2023-09-27 18:26:08

我正在编写Windows服务,它在定义的时间段(现在是20秒)后调用方法。一切都很好,直到没有。似乎找不到问题的原因。服务似乎正确地启动和停止提供日志条目,但似乎并没有调用逝去的事件。

public partial class UssPwdSyncService : ServiceBase
{
    private Timer timer1 = null; 
    public UssPwdSyncService()
    {
        InitializeComponent();
    }
    protected override void OnStart(string[] args)
    {
        try
        {
            timer1 = new Timer();
            this.timer1.Interval = 20000;
            this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_tick);
            timer1.Enabled = true;
            LogHandling.WriteErrorLogs("Service has started! LOg!");
        }
        catch (Exception ex)
        {
            LogHandling.WriteErrorLogs(ex);
        }
    }
    private void timer1_tick(object sender, ElapsedEventArgs e)
    {
        ConfigurationManager.RefreshSection("connectionStrings");
        foreach (ConnectionStringSettings cStr in ConfigurationManager.ConnectionStrings)
        {
            string name = cStr.Name;
            string connString = cStr.ConnectionString;
            string provider = cStr.ProviderName;
            LogHandling.WriteErrorLogs(name + " " + connString + " " + provider);
        }
        LogHandling.WriteErrorLogs("This does something!");
    }
    protected override void OnStop()
    {
        timer1.Enabled = false;
        LogHandling.WriteErrorLogs("Service has stoped!");
    }
}

有人能指出我遗漏了什么吗?

Win服务中的计时器不调用运行事件

我将try-catch移到了timer1_tick方法。这是进行异常检查的正确位置。

您可能会抛出一个异常,以获取代码的平静。你有connectionStrings部分吗?

注意:我更喜欢使用StartStop方法,而不是Enabled = true和CCD_ 5。有两种方法是正确的。

试试这个:

public partial class UssPwdSyncService : ServiceBase
{
    private Timer timer1 = null; 
    public UssPwdSyncService()
    {
        InitializeComponent();
        this.timer1 = new Timer();
        this.timer1.Interval = 20000;
        this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_tick);
    }
    protected override void OnStart(string[] args)
    {
        this.timer1.Start();
        LogHandling.WriteErrorLogs("Service has started! LOg!");
    }
    private void timer1_tick(object sender, ElapsedEventArgs e)
    {
        try
        {
            ConfigurationManager.RefreshSection("connectionStrings");
            foreach (ConnectionStringSettings cStr in ConfigurationManager.ConnectionStrings)
            {
                string name = cStr.Name;
                string connString = cStr.ConnectionString;
                string provider = cStr.ProviderName;
                LogHandling.WriteErrorLogs(name + " " + connString + " " + provider);
            }
            LogHandling.WriteErrorLogs("This does something!");
        }
        catch (Exception ex)
        {
            LogHandling.WriteErrorLogs(ex);
        }
    }
    protected override void OnStop()
    {
        this.timer1.Stop();
        LogHandling.WriteErrorLogs("Service has stoped!");
    }
}

我假设您在System.Timers中使用Timer。您需要调用计时器的Start()方法。