System.Timers.Timer已过去的事件过早触发

时间:2016-03-08 19:54:03

标签: c# .net timer elapsedtime

我遇到了在间隔之前触发Elapsed事件的问题。我将间隔设置为10000 ..并且事件将在大约4500ms处触发。我知道这个特定的计时器不是太精确,但我确实知道它比它显示的更准确。

我已经检查过以确保调用此事件的计时器不会超过一个。此解决方案可以在安装它的三台Windows机器中的两台机器上完美运行。

.net版本,clr版本等可能是个问题吗?

我知道还有其他方法可以实现这一目标,但我只是在寻找可能导致这种情况仅在3台服务器中有2台工作的建议。

下面我在启动服务时只创建一次计时器..

checkTimer = new System.Timers.Timer(getSecondsLeft());
checkTimer.Elapsed += checkNowEvent;
checkTimer.AutoReset = true;
checkTimer.Enabled = true;

以下是用于计算下一分钟之前的毫秒数的方法

private double getSecondsLeft()
{
    DateTime now = DateTime.Now;
    // Has a chance to trigger a few milliseconds before new minute. Added 50ms to interval
    return ((60 - now.Second) * 1000 - now.Millisecond) + 50;
}

最后是经过时间的事件。

private void checkNowEvent(object sender, ElapsedEventArgs e)
{
    try
    {
        // Stop timer to keep from firing this event again
        checkTimer.Enabled = false;

        // DOING WORK HERE
    }
    finally
    {
        // Set the interval as to tick on the start of the next minute
        checkTimer.Interval = getSecondsLeft();

        // Start timer again
        checkTimer.Enabled = true;
    }
}

我刚刚对此进行了一些测试,我添加了一些秒表功能,以查看间隔是否实际正在触发它应该是什么时候它看起来像是。但是,当我计算到下一分钟的正确毫秒数时,它表现得好像Timer的这种实现运行速度比系统时钟快......如果这有意义的话。

以下是我用来找到它的代码。

    private void checkNowEvent(object sender, ElapsedEventArgs e)
    {

        stopWatch.Stop();
        _Log.LogDebug(stopWatch.Elapsed.ToString());

        try
        {

            // Stop timer to keep from firing this event again
            checkTimer.Enabled = false;

            // DOING WORK HERE

        }
        catch (Exception ex)
        {

            // CATCHING EXCEPTIONS HERE IF ANY

        }
        finally
        {
            // Set the interval as to tick on the start of the next minute
            checkTimer.Interval = getSecondsLeft();
            _Log.LogDebug(checkTimer.Interval.ToString());

            // Start timer again
            checkTimer.Enabled = true;

            stopWatch.Reset();
            stopWatch.Start();
        }
    }

1 个答案:

答案 0 :(得分:2)

在新分钟之前触发的原因是由于系统时间有问题。 DateTime.Now返回正确的毫秒数,直到下一分钟,但系统时间移动非常慢。当我用秒表验证时,计时器实现似乎正常工作。我只是将系统时间与其他两个工作电脑同步,但不是5分钟后它又慢了几分钟。