![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Are Threading.Timers Supposed to be Accurate? Perhaps I have misunderstood how these things work. I have a Threading.Timer object set to go off every 10 seconds. The timer callback sends some data out on a TCP socket and checks for a reply. It then terminates. It will only wait for a maximum of 5 seconds, but occasionally it will timeout and exit. If the first callback occurs at time t and takes 5 seconds to complete, I am expecting the next callback to occur at t+10. That's correct, isn't it? What I am finding is that sometimes a callback doesn't occur for as much as 22 seconds after the previous one. What could cause this? Would a System.Timers.Timer be any better? TIA Charles |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Are Threading.Timers Supposed to be Accurate? "Charles" <blank@newsgroup> wrote in message news:e2gveZpVKHA.1280@newsgroup Quote: > Perhaps I have misunderstood how these things work. I have a > Threading.Timer object set to go off every 10 seconds. The timer callback > sends some data out on a TCP socket and checks for a reply. It then > terminates. It will only wait for a maximum of 5 seconds, but occasionally > it will timeout and exit. > > If the first callback occurs at time t and takes 5 seconds to complete, I > am expecting the next callback to occur at t+10. That's correct, isn't it? > > What I am finding is that sometimes a callback doesn't occur for as much > as 22 seconds after the previous one. What could cause this? > > Would a System.Timers.Timer be any better? > > TIA > > Charles > > I'm not sure how dotnet handles timers, but in the underlying Windows API, your first scenario simply can't happen (t+5) for a 10 second timer. Your second scenario can happen in the rare situation where your system is extremely busy and sluggish. Basically, the Windows Timer (WM_TIMER) messages are the second lowest priority messages in the system - only the WM_PAINT message that generates the OnPaint() event is lower and only occurs if there are no other messages waiting to be processed. Please post your timer creation code so we can better help. Mike Ober. |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Are Threading.Timers Supposed to be Accurate? Hi Michael Thanks for the reply. Quote: > your first scenario simply can't happen (t+5) for a 10 second timer. Your seen teh callback report 5 seconds. Here's the code ' Create timer m_Timer = New Threading.Timer(AddressOf TimerCallback, New TimerState, 10000, 10000) And the TimerCallback function is Private Sub PollCallback(ByVal state As Object) SyncLock Me Try Dim TimeNow As DateTime = Now ts = DirectCast(state, TimerState) If Not ts.FirstTime Then Dim ElapsedSeconds As Long = DateDiff(DateInterval.Second, ts.LastCalled, TimeNow) If ElapsedSeconds < 9 Then ' Called too quickly OnTimerError(String.Format("Timer Request queued too quickly, in {0} seconds", ElapsedSeconds)) ElseIf ElapsedSeconds > 11 Then ' Called too slowly OnTimerError(String.Format("Timer Request queued too slowly, in {0} seconds", ElapsedSeconds)) End If End If ts.LastCalled = TimeNow ' assignment clears FirstTime flag ' Do stuff that takes 1 to 5 seconds End SyncLock End Sub Sometimes it reports 5 seconds, sometimes 14, sometimes 25. Mostly it reports nothing. There are other things going on in the app, but mostly just waiting on wait handles for things to be signalled, like messages coming in. CPU never gets abovre a few percent for the whole machine, running Windows Server 2003. Charles "Michael D. Ober" <obermd.@newsgroup> wrote in message news:qs6dnUhCqKw003vXnZ2dnUVZ_uidnZ2d@newsgroup Quote: > "Charles" <blank@newsgroup> wrote in message > news:e2gveZpVKHA.1280@newsgroup Quote: >> Perhaps I have misunderstood how these things work. I have a >> Threading.Timer object set to go off every 10 seconds. The timer callback >> sends some data out on a TCP socket and checks for a reply. It then >> terminates. It will only wait for a maximum of 5 seconds, but >> occasionally it will timeout and exit. >> >> If the first callback occurs at time t and takes 5 seconds to complete, I >> am expecting the next callback to occur at t+10. That's correct, isn't >> it? >> >> What I am finding is that sometimes a callback doesn't occur for as much >> as 22 seconds after the previous one. What could cause this? >> >> Would a System.Timers.Timer be any better? >> >> TIA >> >> Charles >> >> > > I'm not sure how dotnet handles timers, but in the underlying Windows API, > your first scenario simply can't happen (t+5) for a 10 second timer. Your > second scenario can happen in the rare situation where your system is > extremely busy and sluggish. Basically, the Windows Timer (WM_TIMER) > messages are the second lowest priority messages in the system - only the > WM_PAINT message that generates the OnPaint() event is lower and only > occurs if there are no other messages waiting to be processed. > > Please post your timer creation code so we can better help. > > Mike Ober. > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Webcam timers | General Discussion | |||
| Issues with running two timers at the same time | .NET General | |||
| Network Map is *almost* accurate. Help? | Vista networking & sharing | |||
| Date and time are accurate, Reliability Monitor displays year as 1 | Vista performance & maintenance | |||