gpt4 book ai didi

.net - Windows Service System.Timers.Timer无法启动

转载 作者:行者123 更新时间:2023-12-03 08:52:52 24 4
gpt4 key购买 nike

我有一个用C#编写的Windows服务,该服务每隔几分钟执行一次任务。我为此使用了System.Timers.Timer,但它似乎从未触发过。我在SO和其他地方查看了许多不同的文章,但没有发现我的代码有什么问题。

这是我的代码,为清晰起见,删除了与计时器无关的项目...

namespace NovaNotificationService
{
public partial class NovaNotificationService : ServiceBase
{
private System.Timers.Timer IntervalTimer;
public NovaNotificationService()
{
InitializeComponent();
IntervalTimer = new System.Timers.Timer(60000); // Default in case app.config is silent.
IntervalTimer.Enabled = false;
IntervalTimer.Elapsed += new ElapsedEventHandler(this.IntervalTimer_Elapsed);
}

protected override void OnStart(string[] args)
{
// Set up the timer...
IntervalTimer.Enabled = false;
IntervalTimer.Interval = Properties.Settings.Default.PollingFreqInSec * 1000;
// Start the timer and wait for the next work to be released...
IntervalTimer.Start();
}

protected override void OnStop()
{
IntervalTimer.Enabled = false;
}

private void IntervalTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{ // Do the thing that needs doing every few minutes...
DoWork();
}
}
}

我真的很head头。有人能发现我犯错了什么愚蠢的事情吗?

编辑:
根据建议,我在服务OnStart方法中的 IntervalTimer.Enabled = true;之前添加了 IntervalTimer.Start();。这不能解决问题。

我已将文件跟踪记录添加到服务中以确认一些内部信息,并且我确定在OnStart()完成时Timer.Enabled值是正确的。

最佳答案

这是我的解决方法...

在花了太多时间寻找答案之后,我发现了许多讨论Windows服务中计时器的文章和博客。我已经看到了很多关于此的意见,它们都按频率降序分为三类:

  • 不要使用System.Windows.Forms.Timer,因为它不起作用。 (这只说得通)
  • 不要使用System.Threading.Timer,因为它不起作用,请改用System.Timers.Timer
  • 不要使用System.Timers.Timer,因为它不起作用,请改用System.Threading.Timer

  • 基于此,我尝试了2。这也是Microsoft似乎推荐的方法,因为他们说 System.Timers.Timersuited to "Server applications"

    我发现 System.Timers.Timer在Windows Service应用程序中不起作用。因此,我已切换到 System.Threading.Timer。这很麻烦,因为需要进行一些重构才能使其正常工作。

    这大约是我现在的工作代码的样子...
    namespace NovaNotificationService
    {
    public partial class NovaNotificationService : ServiceBase
    {
    private System.Threading.Timer IntervalTimer;
    public NovaNotificationService()
    {
    InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
    TimeSpan tsInterval = new TimeSpan(0, 0, Properties.Settings.Default.PollingFreqInSec);
    IntervalTimer = new System.Threading.Timer(
    new System.Threading.TimerCallback(IntervalTimer_Elapsed)
    , null, tsInterval, tsInterval);
    }

    protected override void OnStop()
    {
    IntervalTimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
    IntervalTimer.Dispose();
    IntervalTimer = null;
    }

    private void IntervalTimer_Elapsed(object state)
    { // Do the thing that needs doing every few minutes...
    // (Omitted for simplicity is sentinel logic to prevent re-entering
    // DoWork() if the previous "tick" has for some reason not completed.)
    DoWork();
    }
    }
    }

    我讨厌“医生,医生,当我这样做时会很痛...”解决方案,但这就是我必须采取的措施。对于这个问题的下一个家伙,还有更多的看法...

    关于.net - Windows Service System.Timers.Timer无法启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8466597/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com