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

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?
kevinmeredithAsked:
Who is Participating?
 
Bob LearnedCommented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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?
0
 
kevinmeredithAuthor 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?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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?
0
 
kevinmeredithAuthor 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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
 
omegaomegaDeveloperCommented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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."
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You didn't find any of my comments useful?    geesh...
0
 
Bob LearnedCommented:
Well, Mike, I do :-P

Bob

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Thanx Bob!  good enough for me...

Can you tell I'm tired and grumpy today?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.