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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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?
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?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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?
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

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.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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.
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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, and Mathematics TeachersCommented:
You didn't find any of my comments useful?    geesh...
Bob LearnedCommented:
Well, Mike, I do :-P

Bob

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Thanx Bob!  good enough for me...

Can you tell I'm tired and grumpy today?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.