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 - How to Make a .NET Timer Always Fire at the Right Time

Reply
 
Old 4 Weeks Ago   #1 (permalink)
Charles


 
 

How to Make a .NET Timer Always Fire at the Right Time

This is a follow up to an earlier post, about a Threading.Timer that
occasionally fired at odd times. In that case I discovered that low memory
meant that the machine 'froze' intermittently and a timer callback could
fire after 30 seconds instead of every 10 seconds as intended.

I now find that if the machine becomes preoccupied with another task, I get
the same effect. This is a very bad state of affairs, as I can no longer
rely on my 10 second tick occurring every 10 seconds.

I need to have a reliable 10 second timer, such that an event happens every
10 seconds. It's no good if I get two events after 20 seconds, I need one
every 10 seconds.

How is this possible to guarantee in .NET? The app is running on Windows
Server 2003 x64.

TIA

Charles



My System SpecsSystem Spec
Old 4 Weeks Ago   #2 (permalink)
Family Tree Mike


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

Charles wrote:
Quote:

> This is a follow up to an earlier post, about a Threading.Timer that
> occasionally fired at odd times. In that case I discovered that low
> memory meant that the machine 'froze' intermittently and a timer
> callback could fire after 30 seconds instead of every 10 seconds as
> intended.
>
> I now find that if the machine becomes preoccupied with another task, I
> get the same effect. This is a very bad state of affairs, as I can no
> longer rely on my 10 second tick occurring every 10 seconds.
>
> I need to have a reliable 10 second timer, such that an event happens
> every 10 seconds. It's no good if I get two events after 20 seconds, I
> need one every 10 seconds.
>
> How is this possible to guarantee in .NET? The app is running on Windows
> Server 2003 x64.
>
> TIA
>
> Charles
>
>
If the task is that critical that it _must_ occur every 10 seconds, then
I would dedicate a machine to that task. If that task is it's job, then
it does nothing else.

It is impossible for me to know your situation, but is there a way to
"throttle" the other process that is grabbing the CPU attention? That
may be an option to look into.

--
Mike
My System SpecsSystem Spec
Old 4 Weeks Ago   #3 (permalink)
Patrice


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

Some more details could help but this is possible only for a real time OS
(http://en.wikipedia.org/wiki/Real-time_operating_system)... Also a common
misuse is to to use a timer to measure the time, if applicable you could get
this timer fire with a possible margin and then get the system time date to
get the actual time you are called.

We can't say much more wihtout knwoing what is the critical task that needs
to be done...

--
Patrice

My System SpecsSystem Spec
Old 4 Weeks Ago   #4 (permalink)
Jeroen Mostert


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

Charles wrote:
Quote:

> This is a follow up to an earlier post, about a Threading.Timer that
> occasionally fired at odd times. In that case I discovered that low
> memory meant that the machine 'froze' intermittently and a timer
> callback could fire after 30 seconds instead of every 10 seconds as
> intended.
>
> I now find that if the machine becomes preoccupied with another task, I
> get the same effect. This is a very bad state of affairs, as I can no
> longer rely on my 10 second tick occurring every 10 seconds.
>
> I need to have a reliable 10 second timer, such that an event happens
> every 10 seconds. It's no good if I get two events after 20 seconds, I
> need one every 10 seconds.
>
> How is this possible to guarantee in .NET? The app is running on Windows
> Server 2003 x64.
>
You can't really do this with managed code only, because all managed timing
mechanisms that aren't WM_TIMER rely on a hand-rolled thread pool which is
very much subject to preemption.

Technically speaking, as others have pointed out, you can't do it in
unmanaged code either, as it requires a real-time OS to do tasks in hard
real-time. That said, unmanaged code *can* get much better accuracy in fixed
timing, because you can use the OS mechanisms. But there is still no cure
for a system that's completely locked up with I/O -- you can't just steal
time from the interrupt handlers.

Doing something every 10 seconds doesn't require high resolution, so an
option is to use a thread running at high or even realtime priority that
sleeps for 500 ms and checks how much time has passed every time it wakes
up. This is still subject to preemption, so how well it works depends.

Another option is to P/Invoke to CreateWaitableTimer() or
CreateTimerQueueTimer() using one of the the WT_EXECUTEIN* flags. I have no
idea how well this holds up when the system is busy, but it ought to do
better than .NET's own thread pool. Getting the interop right can be tricky,
though.

Many people still use the multimedia timer functions (timeSetEvent() and the
like) despite these being nominally obsoleted, as they still offer higher
accuracy -- but with a 10 second period you're not likely to need them.

--
J.
My System SpecsSystem Spec
Old 4 Weeks Ago   #5 (permalink)
Charles


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

Hi Mike

Thanks for the quick reply. It's not critical that it is exactly 10 seconds.
12 seconds would do, or even 15 seconds occasionally. But what I can't have
is it being 30 seconds between timer events, and that is the worst cases I
have seen. I have to send a message over TCP/IP about every 10 seconds in
order to keep a connection open, otherwise the other end shuts up shop and
goes away.

Charles


"Family Tree Mike" <FamilyTreeMike@newsgroup> wrote in message
news:#QdxhSvWKHA.1792@newsgroup
Quote:

> Charles wrote:
Quote:

>> This is a follow up to an earlier post, about a Threading.Timer that
>> occasionally fired at odd times. In that case I discovered that low
>> memory meant that the machine 'froze' intermittently and a timer callback
>> could fire after 30 seconds instead of every 10 seconds as intended.
>>
>> I now find that if the machine becomes preoccupied with another task, I
>> get the same effect. This is a very bad state of affairs, as I can no
>> longer rely on my 10 second tick occurring every 10 seconds.
>>
>> I need to have a reliable 10 second timer, such that an event happens
>> every 10 seconds. It's no good if I get two events after 20 seconds, I
>> need one every 10 seconds.
>>
>> How is this possible to guarantee in .NET? The app is running on Windows
>> Server 2003 x64.
>>
>> TIA
>>
>> Charles
>>
>>
>
> If the task is that critical that it _must_ occur every 10 seconds, then I
> would dedicate a machine to that task. If that task is it's job, then it
> does nothing else.
>
> It is impossible for me to know your situation, but is there a way to
> "throttle" the other process that is grabbing the CPU attention? That may
> be an option to look into.
>
> --
> Mike
My System SpecsSystem Spec
Old 4 Weeks Ago   #6 (permalink)
Charles


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

Hi Jeroen

I understand better now from your explanation, thanks. I might give the
P/Invoke method a try as I've used this before for other comms projects, so
I'm reasonably familiar with getting it working. I presume if I switched
from a Threading timer to a System.Timers.Timer I would be no better off?

Cheers

Charles


"Jeroen Mostert" <jmostert@newsgroup> wrote in message
news:4aedb1d9$0$83234$e4fe514c@newsgroup
Quote:

> Charles wrote:
Quote:

>> This is a follow up to an earlier post, about a Threading.Timer that
>> occasionally fired at odd times. In that case I discovered that low
>> memory meant that the machine 'froze' intermittently and a timer callback
>> could fire after 30 seconds instead of every 10 seconds as intended.
>>
>> I now find that if the machine becomes preoccupied with another task, I
>> get the same effect. This is a very bad state of affairs, as I can no
>> longer rely on my 10 second tick occurring every 10 seconds.
>>
>> I need to have a reliable 10 second timer, such that an event happens
>> every 10 seconds. It's no good if I get two events after 20 seconds, I
>> need one every 10 seconds.
>>
>> How is this possible to guarantee in .NET? The app is running on Windows
>> Server 2003 x64.
>>
> You can't really do this with managed code only, because all managed
> timing mechanisms that aren't WM_TIMER rely on a hand-rolled thread pool
> which is very much subject to preemption.
>
> Technically speaking, as others have pointed out, you can't do it in
> unmanaged code either, as it requires a real-time OS to do tasks in hard
> real-time. That said, unmanaged code *can* get much better accuracy in
> fixed timing, because you can use the OS mechanisms. But there is still no
> cure for a system that's completely locked up with I/O -- you can't just
> steal time from the interrupt handlers.
>
> Doing something every 10 seconds doesn't require high resolution, so an
> option is to use a thread running at high or even realtime priority that
> sleeps for 500 ms and checks how much time has passed every time it wakes
> up. This is still subject to preemption, so how well it works depends.
>
> Another option is to P/Invoke to CreateWaitableTimer() or
> CreateTimerQueueTimer() using one of the the WT_EXECUTEIN* flags. I have
> no idea how well this holds up when the system is busy, but it ought to do
> better than .NET's own thread pool. Getting the interop right can be
> tricky, though.
>
> Many people still use the multimedia timer functions (timeSetEvent() and
> the like) despite these being nominally obsoleted, as they still offer
> higher accuracy -- but with a 10 second period you're not likely to need
> them.
>
> --
> J.
My System SpecsSystem Spec
Old 4 Weeks Ago   #7 (permalink)
Patrice


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

> I have just replied to Mike about what I am doing, but essentially I am
Quote:

> sending a heartbeat over TCP/IP and if it doesn't get sent in time then
> the other end thinks I've died and it abandons the connection.
I'm not a TCP expert but it really looks like the "TCP keepalive" feature.
Have you tried http://msdn.microsoft.com/en-us/library/e160993d.aspx with
the keepalive option ? Also it looks really low ? Is this is third party
application server side ?

--
Patrice

My System SpecsSystem Spec
Old 4 Weeks Ago   #8 (permalink)
Jeroen Mostert


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

Charles wrote:
Quote:

> I presume if I switched from a Threading timer to a System.Timers.Timer I
> would be no better off?
>
You presume correctly. System.Timers.Timer is in fact a wrapper around
System.Threading.Timer with a slightly more conventional API. Except for
System.Windows.Forms timer, which is based on SetTimer() and WM_TIMER, all
managed timers rely on the managed thread pool, which is almost but not
quite like the native OS thread pool.

--
J.
My System SpecsSystem Spec
Old 4 Weeks Ago   #9 (permalink)
Jeroen Mostert


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

Patrice wrote:
Quote:
Quote:

>> I have just replied to Mike about what I am doing, but essentially I
>> am sending a heartbeat over TCP/IP and if it doesn't get sent in time
>> then the other end thinks I've died and it abandons the connection.
>
> I'm not a TCP expert but it really looks like the "TCP keepalive"
> feature.
I'm not a TCP expert either, but I do know TCP keepalives are usually more
trouble than they're worth. You have to change the default keepalive time
(by default it's two hours, which is useless), and even then all the
mechanism does is send a number of probes with 1-second intervals to probe
the other side for connectivity. The packets sent this way are just 0-byte
data packets, which the other side will not necessarily respond correctly to
("responding" in this case is just receiving on the socket and not crashing
on the zero-byte packet; the network layer will take care of the ACK).

It's almost always a better idea to implement an explicit keepalive
mechanism in whatever protocol you're using than to rely on the TCP
keepalive. That's assuming your protocol actually needs keepalives, as many
people forget that TCP's ability to keep a connection open without traffic
is a feature, not a bug.

--
J.
My System SpecsSystem Spec
Old 4 Weeks Ago   #10 (permalink)
Charles


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

I've been wondering how I might increase the priority of the the thread that
the timer runs on, but if it uses the thread pool then I don't imagine I can
increase the priority? If that's the case, then perhaps that's another
reason for going for a waitable timer, as the callback runs on the same
thread on which the timer was created and I could boost the priority of that
thread. Does that sound reasonable?

Charles


"Jeroen Mostert" <jmostert@newsgroup> wrote in message
news:4aedd600$0$83234$e4fe514c@newsgroup
Quote:

> Charles wrote:
Quote:

>> I presume if I switched from a Threading timer to a System.Timers.Timer I
>> would be no better off?
>>
> You presume correctly. System.Timers.Timer is in fact a wrapper around
> System.Threading.Timer with a slightly more conventional API. Except for
> System.Windows.Forms timer, which is based on SetTimer() and WM_TIMER, all
> managed timers rely on the managed thread pool, which is almost but not
> quite like the native OS thread pool.
>
> --
> J.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
What can I do to make sure the time is correct on the computers on our network? Server General
Need to click twice to fire an Event .NET General
DragEnter Does Not Fire? .NET General
chocolate fire guard... Vista General
FIRE FIRE !!!!!Video Games play over second page Vista General


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