We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

1000 ms (1 second) Timer control event actually takes 1.46 seconds

kevinmeredith
on
Medium Priority
1,338 Views
Last Modified: 2008-03-17
I have a VB .NET Timer control.  I set Timer1.Interval to 1000 ms (1 second).  It turns out that the timer event happens every 1.46 seconds!

I've read that the timer control is derived from the 18/second PC clock and cannot, therefore, time anything closer than 55 ms. However, my error (1.46 sec instead of 1.0 seconds) is way bigger than that.  Can anyone explain this phenomenon?

By trial and error I can set the interval to 450 ms and get an event of about 1 second.  But how can I count on that staying that way if it doesn't make sense?
Comment
Watch Question

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Simply put, you CANNOT rely on the Timer control to fire at exactly the specifed interval.

The only thing you are guaranteed is that the Timer will NOT fire BEFORE that specified interval is up.

What are you trying to do in your app?

Author

Commented:
I want to pole some data representing a pushbutton.  I need the 1 second interval to detect holding the pushbutton for 1 second.  I do other monitoring on 1 second intervals also like scale weights.

Why would it be off so much (1000 ms really being 1460 ms)?  Can I rely on 450 ms setting to give a 1 second timer interval?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
You can't rely on any offset value to compensate for what you are seeing.  It is really dependent upon what else is going in the system and what else you have running in your application.  If your app is busy then the Timer event will just sit in the message pump until it gets a "breather" and is able to process those waiting messages.

Do you have any other timers running or some kind of polling loop with a While...Wend?

Author

Commented:
No other timers.  I do have while loops, but they have a  System.Windows.Forms.Application.DoEvents()  in the loop.

You say   "The only thing you are guaranteed is that the Timer will NOT fire BEFORE that specified interval is up."  That's more like I would expect from some kind of Wait function, waiting for some interval to pass and making sure that interval passes.

But Timers are for getting repetitive events, so one would think it would be somewhat accurate.  Also, Timers have been around since day 1 with VB 3 (I think).  I would have thought I would have heard about the uselessness of timers before now.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
So are you trying to get the timer to be accurate at the same time as the While loop that has DoEvents() in it?

If you have a While loop that is processor intensive WITHOUT a DoEvents() in it then the Timer event would not be processed at all until the loop drops out.

When you place DoEvents in the loop it processes all pending msgs in the pump.  If the code in the loop before the DoEvents() takes longer than the Interval, or if the code is actually running when the Timer is fired then you won't see that Timer event actually trigger in your code until the DoEvents is hit and the pending msgs are processed.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
There are 3 different types of timers:

http://www.developer.com/net/net/article.php/3335731

Did you try the other 2 (System.Timers.Timer or System.Threading.Timer)?

Bob

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
omegaomegaDeveloper
CERTIFIED EXPERT
Commented:
Hello, Kevin,

I think that the fundamental problem here is that you are expecting Windows to be a "real-time" operating system.  It's not.  You will need to move into a completely different computing arena for that.

However, if you don't need the timer to work accurately ALL of the time (just most of the time) you should try Bob's suggestion.  Also, I think that Windows provides opportunities to play with priorities.  I never have, but you might investigate this to make the time intervals more consistent.

But with Windows, time intervals will never be 100% consistent.  The operating system was not designed for that purpose.  See

    http://www.codeproject.com/system/simpletime.asp

for more information.

Cheers,
Randy
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
I liked the article that Randy linked.

Again, I will repeat what I said earlier in my very first post (which was your very first response):

    "The only thing you are guaranteed is that the Timer will NOT fire BEFORE that specified interval is up."
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
You didn't find any of my comments useful?    geesh...
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Well, Mike, I do :-P

Bob

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Thanx Bob!  good enough for me...

Can you tell I'm tired and grumpy today?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.