?
Solved

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

Posted on 2006-03-23
11
Medium Priority
?
1,168 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?
0
Comment
Question by:kevinmeredith
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16274773
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
 

Author Comment

by:kevinmeredith
ID: 16274853
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16275890
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kevinmeredith
ID: 16276109
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16276940
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1400 total points
ID: 16277229
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
 
LVL 12

Assisted Solution

by:omegaomega
omegaomega earned 600 total points
ID: 16278220
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16280284
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16315190
You didn't find any of my comments useful?    geesh...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16315349
Well, Mike, I do :-P

Bob

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16315388
Thanx Bob!  good enough for me...

Can you tell I'm tired and grumpy today?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question