none
Timer Elapsed event firing slower each time RRS feed

  • Question

  • Hello,

    We have setup a timer to fire an event every interval number of minutes.

    However, we noticed that when we check the current time that the event fires, it is slowly getting more delayed with each execution of the timer event.

    It will continue until it then clicks over to the next second, and therefore the timer is no longer executing from its scheduled interval, if its original time of execution is the reference point.

    What can we do to ensure that the timer event executes at the exact second every time?

    Here is the setup code for the timer:

    # Setup the monitoring timer, every $MonitorInterval_ms milliseconds (minutes).
    $MonitorTimer = New-Object Timers.Timer
    $MonitorTimer.Interval = $global:MonitorInterval_ms
    $MonitorTimer.Enabled = $true
    
    
    # Create event to trigger event and add an action to run when the time elapses.
    $MonitorTimerEvent = Register-ObjectEvent -InputObject $MonitorTimer -EventName Elapsed -SourceIdentifier $MonitorTimer_SourceId -Action {
    
    # Code here which checks current time and prints it out ...
    
    }
    
    # Start the monitoring timer.
    $MonitorTimer.start( )


    In the screenshot, $global:MonitorInterval_ms was set to 1 minute, and therefore the timer event should fire every 1 minute, which it does.

    However, the time continues to become more delayed and will eventually clock over to the next second.

    Therefore, the more the timer executes, the more the interval will continue to execute at a later time than expected.

    Sorry, my account is not verified yet so I cannot post a screenshot to show the effect yet.

    Thank you

    Edward

    Thursday, March 31, 2016 4:21 PM

Answers

All replies

  • It is not a high resolution timer.  Short intervals will b e very unreliable.


    \_(ツ)_/

    Thursday, March 31, 2016 6:25 PM
  • Thank you for the reply. We should probably use a scheduled time instead then, so we do not lose the exact time we want to execute on. The challenge then will be to ensure the scheduled action has all the data it needs, if it is going to execute independently of the Powershell script. It also has a separate routine which is periodically checking data changes, which will need to interact with and report to the scheduled action. This data will probably need to be saved somewhere for retrieval by the scheduled action.
    Friday, April 1, 2016 8:35 AM