How to Generate an Accurate Timer Tick

  • Thread starter Thread starter Charles
  • Start date Start date
C

Charles

I asked a question related to this a little while ago, and thought that I'd
got my answer, but it has come back to bite me again.

I currently use a System.Threading.Timer to generate a tick every 10
seconds. At each tick, I execute some code that will take a maximum of 5
seconds to complete. Most of the time, each subsequent tick occurs at
exactly 10 seconds after the previous one, but occasionally there can be as
much as 20 or 30 seconds between ticks.

It was explained, in the previous thread, that the Threading timer relies on
WM_TIMER messages, which are low down on the priority list. If the system
gets a bit busy then these message seem to come further apart, so my tick
interval extends.

What I need is a reliable way to generate a 10 second tick, that still works
when the system gets a bit busy. I'm running this on Windows Server 2003 R2
x64, if that makes any difference.

Does anyone have any ideas?

TIA

Charles
 
I asked a question related to this a little while ago, and thought that
I'd got my answer, but it has come back to bite me again.

I currently use a System.Threading.Timer to generate a tick every 10
seconds. At each tick, I execute some code that will take a maximum of 5
seconds to complete. Most of the time, each subsequent tick occurs at
exactly 10 seconds after the previous one, but occasionally there can be
as much as 20 or 30 seconds between ticks.

It was explained, in the previous thread, that the Threading timer
relies on WM_TIMER messages, which are low down on the priority list. If
the system gets a bit busy then these message seem to come further
apart, so my tick interval extends.

What I need is a reliable way to generate a 10 second tick, that still
works when the system gets a bit busy. I'm running this on Windows
Server 2003 R2 x64, if that makes any difference.

Does anyone have any ideas?

TIA

Charles

To get exactly 10 seconds between ticks, you need a real-time operating
system. Windows is not a real-time OS. You will need to rethink the
requirement, or rethink the computing platform.
 
As Mike said, Windows is not a real-time OS. Thus, you cannot guarantee
response.

That said, you may be able to approach the accuracy that you desire. One
thing to realize is that resolution and accuracy are not the same thing.
And, since you will be writing code that is multi-threaded (and you have
only gross control of the various thread execution), and, in addition, are
executing in a non-deterministic OS, you have to careful what you ask for --
and understand that you can never be absolutely certain that you will get
the precision that you seek.

If you use a timer, you depend on multiple things, including the wm_timer
message. You might simply create a thread that monitors elapsed
milliseconds (example: Now.TimeOfDay.TotalMilliseconds). This will be more
accurate than the built-in timer. It will not, however, provide any real
determinism, so at the end of the day, you have to test it under load and
see if it meets your needs. If not... You may not be able to get there from
here.

Dick
--
Richard Grier, Consultant, Hard & Software 12962 West Louisiana Avenue
Lakewood, CO 80228 303-986-2179 (voice) Homepage: www.hardandsoftware.net
Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004, Revised July
2006.
 
Hi Mike

Thanks for the reply. To be honest, I'd be happy with 10 seconds +/- 5
seconds, which doesn't seem to much to ask. Or is it?

Charles
 
Hi Dave

Yes, I have. It was my understanding that the Timers timer was just a
wrapper for the Threading timer. Perhaps not. There doesn't seem to be
anything there that suggests it is any more reliable the the threading
version. If it doesn't use WM_TIMER messages, do you know how it does work?

Thanks

Charles


Davej said:
[...] Most of the time, each subsequent tick occurs at
exactly 10 seconds after the previous one, but occasionally
there can be as much as 20 or 30 seconds between ticks.

You've looked here?

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
 
Hi Dick

As I replied to Mike, 10 +/- 5 seconds would do me. I have to send a
keep-alive message to another system approximately every 10 seconds. The
other end will time-out after 25 seconds or so, which doesn't seem too tight
a window, and yet sometimes the interval between ticks can be 30 seconds or
mor. I wondered about monitoring elapsed seconds, but it just seemed a bit
kludgy. I can't imagine I'm the only person that does this sort of thing in
Windows. How do other applications do it, I wonder, where they have to
perform tasks at regular intervals, with a reasonable degree of accuracy?

Charles
 
Then why not use the standard Windows.Forms.Form.Timer, somehow persons want
to use the system.timer.timer (the windows service one) or the threading
timer (who is able to be used in async)

I am glad that you are not writing of the current fourth one the dispatcher
timer.

People always write about all timers beside the windows.forms.form.timer
that the others are better, but never tell why those are better.

The windows.forms.form timer is at least the most reliable one in most
situations

Cor

Charles said:
Hi Dave

Yes, I have. It was my understanding that the Timers timer was just a
wrapper for the Threading timer. Perhaps not. There doesn't seem to be
anything there that suggests it is any more reliable the the threading
version. If it doesn't use WM_TIMER messages, do you know how it does
work?

Thanks

Charles


Davej said:
[...] Most of the time, each subsequent tick occurs at
exactly 10 seconds after the previous one, but occasionally
there can be as much as 20 or 30 seconds between ticks.

You've looked here?

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
 
Hi Cor, good to hear from you.

I have read that the Forms timer is actually less reliable in terms of its
interval than the other timers, because it is single-threaded. Coupled with
the fact that I want to use this in a service eventually, I don't think I
can use the Forms timer.

Cheers

Charles


Cor Ligthert said:
Then why not use the standard Windows.Forms.Form.Timer, somehow persons
want to use the system.timer.timer (the windows service one) or the
threading timer (who is able to be used in async)

I am glad that you are not writing of the current fourth one the
dispatcher timer.

People always write about all timers beside the windows.forms.form.timer
that the others are better, but never tell why those are better.

The windows.forms.form timer is at least the most reliable one in most
situations

Cor

Charles said:
Hi Dave

Yes, I have. It was my understanding that the Timers timer was just a
wrapper for the Threading timer. Perhaps not. There doesn't seem to be
anything there that suggests it is any more reliable the the threading
version. If it doesn't use WM_TIMER messages, do you know how it does
work?

Thanks

Charles


Davej said:
[...] Most of the time, each subsequent tick occurs at
exactly 10 seconds after the previous one, but occasionally
there can be as much as 20 or 30 seconds between ticks.


You've looked here?

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
 
There is also written that American cars are better than German cars.

Do you believe everything which is written without anything which explains
why?

Take a look at this page which compares timers.

http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

You can handle them top down like in this page as long as something where
they don't work fail.

Cor



Charles said:
Hi Cor, good to hear from you.

I have read that the Forms timer is actually less reliable in terms of its
interval than the other timers, because it is single-threaded. Coupled
with the fact that I want to use this in a service eventually, I don't
think I can use the Forms timer.

Cheers

Charles


Cor Ligthert said:
Then why not use the standard Windows.Forms.Form.Timer, somehow persons
want to use the system.timer.timer (the windows service one) or the
threading timer (who is able to be used in async)

I am glad that you are not writing of the current fourth one the
dispatcher timer.

People always write about all timers beside the windows.forms.form.timer
that the others are better, but never tell why those are better.

The windows.forms.form timer is at least the most reliable one in most
situations

Cor

Charles said:
Hi Dave

Yes, I have. It was my understanding that the Timers timer was just a
wrapper for the Threading timer. Perhaps not. There doesn't seem to be
anything there that suggests it is any more reliable the the threading
version. If it doesn't use WM_TIMER messages, do you know how it does
work?

Thanks

Charles


[...] Most of the time, each subsequent tick occurs at
exactly 10 seconds after the previous one, but occasionally
there can be as much as 20 or 30 seconds between ticks.


You've looked here?

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
 
Have you actually read the article, Cor? I have. It bears out exactly what I
said: "System.Windows.Forms.Timer
If you're looking for a metronome, you've come to the wrong place."

Charles


Cor Ligthert said:
There is also written that American cars are better than German cars.

Do you believe everything which is written without anything which explains
why?

Take a look at this page which compares timers.

http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

You can handle them top down like in this page as long as something where
they don't work fail.

Cor



Charles said:
Hi Cor, good to hear from you.

I have read that the Forms timer is actually less reliable in terms of
its interval than the other timers, because it is single-threaded.
Coupled with the fact that I want to use this in a service eventually, I
don't think I can use the Forms timer.

Cheers

Charles


Cor Ligthert said:
Then why not use the standard Windows.Forms.Form.Timer, somehow persons
want to use the system.timer.timer (the windows service one) or the
threading timer (who is able to be used in async)

I am glad that you are not writing of the current fourth one the
dispatcher timer.

People always write about all timers beside the windows.forms.form.timer
that the others are better, but never tell why those are better.

The windows.forms.form timer is at least the most reliable one in most
situations

Cor

Hi Dave

Yes, I have. It was my understanding that the Timers timer was just a
wrapper for the Threading timer. Perhaps not. There doesn't seem to be
anything there that suggests it is any more reliable the the threading
version. If it doesn't use WM_TIMER messages, do you know how it does
work?

Thanks

Charles


[...] Most of the time, each subsequent tick occurs at
exactly 10 seconds after the previous one, but occasionally
there can be as much as 20 or 30 seconds between ticks.


You've looked here?

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
 
That was the first sentence, did you read all of it?

While I advices you already earlier to use in a windows service a windows
timer timer, because in that a windows forms timer does not go.

You where not asking about picoseconds precise timer, that has been
elaborated already more then enough in this thread.

But if you don't want to use the windows timer timer, feel free, it cost me
at least nothing.

Cor

Charles said:
Have you actually read the article, Cor? I have. It bears out exactly what
I said: "System.Windows.Forms.Timer
If you're looking for a metronome, you've come to the wrong place."

Charles


Cor Ligthert said:
There is also written that American cars are better than German cars.

Do you believe everything which is written without anything which
explains why?

Take a look at this page which compares timers.

http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

You can handle them top down like in this page as long as something where
they don't work fail.

Cor



Charles said:
Hi Cor, good to hear from you.

I have read that the Forms timer is actually less reliable in terms of
its interval than the other timers, because it is single-threaded.
Coupled with the fact that I want to use this in a service eventually, I
don't think I can use the Forms timer.

Cheers

Charles


Then why not use the standard Windows.Forms.Form.Timer, somehow persons
want to use the system.timer.timer (the windows service one) or the
threading timer (who is able to be used in async)

I am glad that you are not writing of the current fourth one the
dispatcher timer.

People always write about all timers beside the
windows.forms.form.timer that the others are better, but never tell why
those are better.

The windows.forms.form timer is at least the most reliable one in most
situations

Cor

Hi Dave

Yes, I have. It was my understanding that the Timers timer was just a
wrapper for the Threading timer. Perhaps not. There doesn't seem to be
anything there that suggests it is any more reliable the the threading
version. If it doesn't use WM_TIMER messages, do you know how it does
work?

Thanks

Charles


[...] Most of the time, each subsequent tick occurs at
exactly 10 seconds after the previous one, but occasionally
there can be as much as 20 or 30 seconds between ticks.


You've looked here?

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
 
I forgot to ask you.

How is your American car going at 200 miles an hour?

Cor

Charles said:
Have you actually read the article, Cor? I have. It bears out exactly what
I said: "System.Windows.Forms.Timer
If you're looking for a metronome, you've come to the wrong place."

Charles


Cor Ligthert said:
There is also written that American cars are better than German cars.

Do you believe everything which is written without anything which
explains why?

Take a look at this page which compares timers.

http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

You can handle them top down like in this page as long as something where
they don't work fail.

Cor



Charles said:
Hi Cor, good to hear from you.

I have read that the Forms timer is actually less reliable in terms of
its interval than the other timers, because it is single-threaded.
Coupled with the fact that I want to use this in a service eventually, I
don't think I can use the Forms timer.

Cheers

Charles


Then why not use the standard Windows.Forms.Form.Timer, somehow persons
want to use the system.timer.timer (the windows service one) or the
threading timer (who is able to be used in async)

I am glad that you are not writing of the current fourth one the
dispatcher timer.

People always write about all timers beside the
windows.forms.form.timer that the others are better, but never tell why
those are better.

The windows.forms.form timer is at least the most reliable one in most
situations

Cor

Hi Dave

Yes, I have. It was my understanding that the Timers timer was just a
wrapper for the Threading timer. Perhaps not. There doesn't seem to be
anything there that suggests it is any more reliable the the threading
version. If it doesn't use WM_TIMER messages, do you know how it does
work?

Thanks

Charles


[...] Most of the time, each subsequent tick occurs at
exactly 10 seconds after the previous one, but occasionally
there can be as much as 20 or 30 seconds between ticks.


You've looked here?

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
 
I don't remember you like this Cor. Is that really you? What have you done
with the real Cor?


Cor Ligthert said:
I forgot to ask you.

How is your American car going at 200 miles an hour?

Cor

Charles said:
Have you actually read the article, Cor? I have. It bears out exactly
what I said: "System.Windows.Forms.Timer
If you're looking for a metronome, you've come to the wrong place."

Charles


Cor Ligthert said:
There is also written that American cars are better than German cars.

Do you believe everything which is written without anything which
explains why?

Take a look at this page which compares timers.

http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

You can handle them top down like in this page as long as something
where they don't work fail.

Cor



Hi Cor, good to hear from you.

I have read that the Forms timer is actually less reliable in terms of
its interval than the other timers, because it is single-threaded.
Coupled with the fact that I want to use this in a service eventually,
I don't think I can use the Forms timer.

Cheers

Charles


Then why not use the standard Windows.Forms.Form.Timer, somehow
persons want to use the system.timer.timer (the windows service one)
or the threading timer (who is able to be used in async)

I am glad that you are not writing of the current fourth one the
dispatcher timer.

People always write about all timers beside the
windows.forms.form.timer that the others are better, but never tell
why those are better.

The windows.forms.form timer is at least the most reliable one in most
situations

Cor

Hi Dave

Yes, I have. It was my understanding that the Timers timer was just a
wrapper for the Threading timer. Perhaps not. There doesn't seem to
be anything there that suggests it is any more reliable the the
threading version. If it doesn't use WM_TIMER messages, do you know
how it does work?

Thanks

Charles


[...] Most of the time, each subsequent tick occurs at
exactly 10 seconds after the previous one, but occasionally
there can be as much as 20 or 30 seconds between ticks.


You've looked here?

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
 
Is it Charles Law?

Charles said:
I don't remember you like this Cor. Is that really you? What have you done
with the real Cor?


Cor Ligthert said:
I forgot to ask you.

How is your American car going at 200 miles an hour?

Cor

Charles said:
Have you actually read the article, Cor? I have. It bears out exactly
what I said: "System.Windows.Forms.Timer
If you're looking for a metronome, you've come to the wrong place."

Charles


There is also written that American cars are better than German cars.

Do you believe everything which is written without anything which
explains why?

Take a look at this page which compares timers.

http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

You can handle them top down like in this page as long as something
where they don't work fail.

Cor



Hi Cor, good to hear from you.

I have read that the Forms timer is actually less reliable in terms of
its interval than the other timers, because it is single-threaded.
Coupled with the fact that I want to use this in a service eventually,
I don't think I can use the Forms timer.

Cheers

Charles


Then why not use the standard Windows.Forms.Form.Timer, somehow
persons want to use the system.timer.timer (the windows service one)
or the threading timer (who is able to be used in async)

I am glad that you are not writing of the current fourth one the
dispatcher timer.

People always write about all timers beside the
windows.forms.form.timer that the others are better, but never tell
why those are better.

The windows.forms.form timer is at least the most reliable one in
most situations

Cor

Hi Dave

Yes, I have. It was my understanding that the Timers timer was just
a wrapper for the Threading timer. Perhaps not. There doesn't seem
to be anything there that suggests it is any more reliable the the
threading version. If it doesn't use WM_TIMER messages, do you know
how it does work?

Thanks

Charles


[...] Most of the time, each subsequent tick occurs at
exactly 10 seconds after the previous one, but occasionally
there can be as much as 20 or 30 seconds between ticks.


You've looked here?

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
 
Seems to be,

However, you wrote that you had written that you had
heard..........................................

That is so often written about that threading timer without any reason, more
that it seems to sound very elite.

Like I've always have been, I don't like statements which goes around simple
because someone had a dream and thought he had to write about it.

However, if I would have known, it was you, my tone would have been very
different.

:-)

Cor

Charles said:
I don't remember you like this Cor. Is that really you? What have you done
with the real Cor?


Cor Ligthert said:
I forgot to ask you.

How is your American car going at 200 miles an hour?

Cor

Charles said:
Have you actually read the article, Cor? I have. It bears out exactly
what I said: "System.Windows.Forms.Timer
If you're looking for a metronome, you've come to the wrong place."

Charles


There is also written that American cars are better than German cars.

Do you believe everything which is written without anything which
explains why?

Take a look at this page which compares timers.

http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

You can handle them top down like in this page as long as something
where they don't work fail.

Cor



Hi Cor, good to hear from you.

I have read that the Forms timer is actually less reliable in terms of
its interval than the other timers, because it is single-threaded.
Coupled with the fact that I want to use this in a service eventually,
I don't think I can use the Forms timer.

Cheers

Charles


Then why not use the standard Windows.Forms.Form.Timer, somehow
persons want to use the system.timer.timer (the windows service one)
or the threading timer (who is able to be used in async)

I am glad that you are not writing of the current fourth one the
dispatcher timer.

People always write about all timers beside the
windows.forms.form.timer that the others are better, but never tell
why those are better.

The windows.forms.form timer is at least the most reliable one in
most situations

Cor

Hi Dave

Yes, I have. It was my understanding that the Timers timer was just
a wrapper for the Threading timer. Perhaps not. There doesn't seem
to be anything there that suggests it is any more reliable the the
threading version. If it doesn't use WM_TIMER messages, do you know
how it does work?

Thanks

Charles


[...] Most of the time, each subsequent tick occurs at
exactly 10 seconds after the previous one, but occasionally
there can be as much as 20 or 30 seconds between ticks.


You've looked here?

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
 
Hi Cor, yes it's me. I've been off in SQL Server newsgroups for a bit and
Windows Server. Doing lots of databasey stuff. Good to speak with you again.
I'm pleased you remember me.

Cheers

Charles


Cor Ligthert said:
Seems to be,

However, you wrote that you had written that you had
heard..........................................

That is so often written about that threading timer without any reason,
more that it seems to sound very elite.

Like I've always have been, I don't like statements which goes around
simple because someone had a dream and thought he had to write about it.

However, if I would have known, it was you, my tone would have been very
different.

:-)

Cor

Charles said:
I don't remember you like this Cor. Is that really you? What have you
done with the real Cor?


Cor Ligthert said:
I forgot to ask you.

How is your American car going at 200 miles an hour?

Cor

Have you actually read the article, Cor? I have. It bears out exactly
what I said: "System.Windows.Forms.Timer
If you're looking for a metronome, you've come to the wrong place."

Charles


There is also written that American cars are better than German cars.

Do you believe everything which is written without anything which
explains why?

Take a look at this page which compares timers.

http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

You can handle them top down like in this page as long as something
where they don't work fail.

Cor



Hi Cor, good to hear from you.

I have read that the Forms timer is actually less reliable in terms
of its interval than the other timers, because it is single-threaded.
Coupled with the fact that I want to use this in a service
eventually, I don't think I can use the Forms timer.

Cheers

Charles


Then why not use the standard Windows.Forms.Form.Timer, somehow
persons want to use the system.timer.timer (the windows service one)
or the threading timer (who is able to be used in async)

I am glad that you are not writing of the current fourth one the
dispatcher timer.

People always write about all timers beside the
windows.forms.form.timer that the others are better, but never tell
why those are better.

The windows.forms.form timer is at least the most reliable one in
most situations

Cor

Hi Dave

Yes, I have. It was my understanding that the Timers timer was just
a wrapper for the Threading timer. Perhaps not. There doesn't seem
to be anything there that suggests it is any more reliable the the
threading version. If it doesn't use WM_TIMER messages, do you know
how it does work?

Thanks

Charles


[...] Most of the time, each subsequent tick occurs at
exactly 10 seconds after the previous one, but occasionally
there can be as much as 20 or 30 seconds between ticks.


You've looked here?

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
 
I'm going to attempt to answer this myself, as I've had an idea.

Rather than try to generate a reliable timer tick, that won't get deferred
when the system gets busy, how about generating a time-out instead. I can
think of two obvious ways of doing it:

1. Put the timed task on it's own thread and go to sleep for the timer tick
interval
2. Also on a separate thread, wait on a handle that never gets set, and set
the timeout to the timer tick interval

I don't know if both methods amount to the same thing, but I would be
interested in people's opinions.

In the first scenario, I spin up a dedicated thread on which I intend to
perform my timed activity at a regular interval. I immediately put the
thread to sleep for my elapsed time, wake up, perform my task and go back to
sleep. This continues forever.

In the second scenario, I create a ManualResetEvent and use WaitOne to wait
for it to be signalled. I set the timeout to my elapsed time again. Each
time the timeout expires I perform my task, and then go back to waiting
again.

In each case, does the technique behave differently from the way a timer
works? In particular, do either or both of them avoid WM_TIMER events? Are
either of these going to give me a more stable and reliable interval,
bearing in mind that I don't mind the interval generated being +/-50%, but I
don't want it to ever be +200%, for example?

I have tried both, and as techniques they work, but testing in the case
where the system is busy and the interval becomes extended is harder to test
empirically.

Charles
 
Back
Top