System.Timers.Timer elapsed event fires too early
<p>I'm running into an issue where the Elapsed event is triggered prior to the interval. I have the interval set for say.. 10000ms and the event will get triggered at roughly 4500ms. I know that this specific timer is not too precise but I do know for sure that it is far more precise than what it is showing.</p>

<p>I have checked to make sure that there is not more than one timer calling this event as well. This solution works perfectly on two out of the three windows machines that it is installed on.</p>

<p>Could it be a problem with the .net version, clr version, etc.</p>

<p>I know that there are other ways of accomplishing this but I am just looking for suggestions on what could be causing this to work on only 2 out of 3 servers.</p>

<p>Below I am creating the timer only once at startup of the service..</p>

<pre><code>checkTimer = new System.Timers.Timer(getSecondsLeft());
checkTimer.Elapsed += checkNowEvent;
checkTimer.AutoReset = true;
checkTimer.Enabled = true;
</code></pre>

<p>Here is the method that is used to calculate the number of milliseconds until the next minute</p>

<pre><code>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;
}
</code></pre>

<p>And finally the elapsed time event.</p>

<pre><code>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;
    }
}
</code></pre>

<p>I just did some more testing with this and I added some stopwatch functionality to see if the interval was actually firing when it is supposed to and it looks like it is. However, when i calculate the correct number of milliseconds to the next minute BUT it is acting as if this implementation of Timer is running faster than the system clock... If that makes any sense.</p>

<p>Here is the code i used to find that out.</p>

<pre><code>    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();
        }
    }
</code></pre>