System.Timers.Timer elapsed event fires too early

H. Andrew

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.

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.

Could it be a problem with the .net version, clr version, etc.

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.

Below I am creating the timer only once at startup of the service..

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

Here is the method that is used to calculate the number of milliseconds until the next minute

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;
}

And finally the elapsed time event.

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;
    }
}

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.

Here is the code i used to find that out.

    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();
        }
    }
H. Andrew

The reason that it is firing before the new minute is due to the fact that there is a problem with the system time. DateTime.Now returns the correct number of milliseconds until the next minute but the system time is moving very slow. The timer implementation seems to be working correctly as I verified with the stopwatch. I just synced the system time with the other two working pcs and not but 5 minutes later it is a couple minutes slow again.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replacing the System.Timers.Timer.Elapsed event when inheriting System.Timers.Timer

From Dev

Why does System.Timers.Timer create multiple threads when it fires the Elapsed events?

From Dev

System.Timers.Timer Elapsed event executing after timer.Stop() is called

From Dev

System.Timers.Timer: How to raise elapsed event after start interval?

From Dev

System.Timers.Timer() Firing multiple times due to aggregation of Elapsed Events

From Dev

C++/CLI System.Timers.Timer elapsed 20 in seconds while set Interval to 100ms

From Dev

System.Timers.Timer() Firing multiple times due to aggregation of Elapsed Events

From Dev

Is it okay to attach async event handler to System.Timers.Timer?

From Dev

WiX Heat: Pre-build event fires too early on build server

From Dev

Javascript img change statement fires too early

From Dev

deferred.always() fires too early

From Dev

How to prevent a C# System.Timers timer event from blocking subsequent events?

From Dev

Timer in Win service doesnt call elapsed event

From Dev

Timer not running all elapsed event handler

From Dev

How to use Timer Elapsed event in Thread

From Java

System.Timers.Timer vs System.Threading.Timer

From Dev

System.Timers.Timer steadily increasing the interval

From Dev

system.timers.timer autoreset default value

From Dev

System.Timers.Timer strange behaviour

From Dev

System.Timers.Timer gradually increasing interval?

From Dev

Mongodb stream call 'end' event too early

From Dev

Change event triggering too early on select box

From Dev

Countdown timer in a thread using System.Timers.Timer

From Dev

Why Timer still fire Elapsed event even after assigning null to it

From Dev

How to redirect to action result from timer elapsed event in mvc

From Dev

How to redirect to action result from timer elapsed event in mvc

From Dev

Update Button's state on Timer's Elapsed Event

From Dev

Simple example of the use of System. Timers. Timer in C#

From Dev

Does System.Timers.Timer terminates on aborting its working Thread?

Related Related

  1. 1

    Replacing the System.Timers.Timer.Elapsed event when inheriting System.Timers.Timer

  2. 2

    Why does System.Timers.Timer create multiple threads when it fires the Elapsed events?

  3. 3

    System.Timers.Timer Elapsed event executing after timer.Stop() is called

  4. 4

    System.Timers.Timer: How to raise elapsed event after start interval?

  5. 5

    System.Timers.Timer() Firing multiple times due to aggregation of Elapsed Events

  6. 6

    C++/CLI System.Timers.Timer elapsed 20 in seconds while set Interval to 100ms

  7. 7

    System.Timers.Timer() Firing multiple times due to aggregation of Elapsed Events

  8. 8

    Is it okay to attach async event handler to System.Timers.Timer?

  9. 9

    WiX Heat: Pre-build event fires too early on build server

  10. 10

    Javascript img change statement fires too early

  11. 11

    deferred.always() fires too early

  12. 12

    How to prevent a C# System.Timers timer event from blocking subsequent events?

  13. 13

    Timer in Win service doesnt call elapsed event

  14. 14

    Timer not running all elapsed event handler

  15. 15

    How to use Timer Elapsed event in Thread

  16. 16

    System.Timers.Timer vs System.Threading.Timer

  17. 17

    System.Timers.Timer steadily increasing the interval

  18. 18

    system.timers.timer autoreset default value

  19. 19

    System.Timers.Timer strange behaviour

  20. 20

    System.Timers.Timer gradually increasing interval?

  21. 21

    Mongodb stream call 'end' event too early

  22. 22

    Change event triggering too early on select box

  23. 23

    Countdown timer in a thread using System.Timers.Timer

  24. 24

    Why Timer still fire Elapsed event even after assigning null to it

  25. 25

    How to redirect to action result from timer elapsed event in mvc

  26. 26

    How to redirect to action result from timer elapsed event in mvc

  27. 27

    Update Button's state on Timer's Elapsed Event

  28. 28

    Simple example of the use of System. Timers. Timer in C#

  29. 29

    Does System.Timers.Timer terminates on aborting its working Thread?

HotTag

Archive