Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Are Threading.Timers Supposed to be Accurate?

Reply
 
Old 10-26-2009   #1 (permalink)
Charles


 
 

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 SpecsSystem Spec
Old 10-26-2009   #2 (permalink)
Michael D. Ober


 
 

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 SpecsSystem Spec
Old 10-26-2009   #3 (permalink)
Charles


 
 

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
I meant that the callback takes 5 seconds to do its work, but below I have
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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46