Results 1 to 8 of 8

Thread: Timer Accuracy

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    17

    Timer Accuracy

    I need to make my program have a timer that is in .1 second intervals. My current program is in 1 second intervals. I am currently using a Timer with the interval set to 1000. I tried changing the interval to 100 when I did this a 3 second program took 5 seconds.
    What the program currently does is every interval looks at a MSHFlexGrid and read across (5 to 200) cells. Based on the sell it desides if it needs to display somthing on the screen.
    Last edited by Ron22; Mar 18th, 2008 at 08:10 AM.

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Timer Accuracy

    Timers are not that accurate in the short term but fairly accurate in the long term. Basically if you counted the calls of a Timer set to a second over 24hrs it would most probably return 86400 but the duration between each call would vary, it's average would be very close to a second.

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Timer Accuracy

    Quote Originally Posted by Ron22
    I need to make my program have a timer that is in .1 second intervals. My current program is in 1 second intervals. I am currently using a Timer with the interval set to 1000. I tried changing the interval to 100 when I did this a 30 second program took 5 seconds.
    What the program currently does is every interval looks at a MSHFlexGrid and read across (5 to 200) cells. Based on the sell it desides if it needs to display somthing on the screen.
    Are you using timer with 1 second interval for other processes? In which case you wouldn't want to change interval for other processing (leave them at 1 sec) and instead would create another timer with interval set to 100 ms and do your 100ms processing there. Your 30 second program should then run at 30 second rather around tenth of 30sec (100ms/1000ms = 1/10)

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    17

    Re: Timer Accuracy

    My current program runs in 1 second intervals. I need to switch it to .1 second intervals. I had a type in my original message it should have been my 3 second program took 5 seconds. (I fixed my original post)
    What can I use in place of a timer?

  5. #5
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Timer Accuracy

    Quote Originally Posted by Ron22
    My current program runs in 1 second intervals. I need to switch it to .1 second intervals. I had a type in my original message it should have been my 3 second program took 5 seconds. (I fixed my original post)
    What can I use in place of a timer?
    This question comes up about once every week. Follow this link and make your own decisions on how to proceed.

    http://www.vbforums.com/showthread.p...ighlight=Pause

    Btw, the Pause Sub uses the Timer Function. It is basically the code that MSDN provided in the help files that's been manipulated into a user defined sub. It can be called from any sub or any event.

    Rant: It's always bugged me that VB didn't carry over the Sleep Statement from QB, and no, the Sleep API isn't an equivalent!
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Timer Accuracy

    One thing about timers... If the timer is informing your app via a WM_TIMER message (as VB timers & SetTimer APIs do), it is difficult to rely on getting an event exactly when expected. Why? The WM_TIMER message is not a high priority message
    Quote Originally Posted by MSDN
    WM_Timer Link.
    The WM_TIMER message is a low-priority message. The GetMessage and PeekMessage functions post this message only when no other higher-priority messages are in the thread's message queue.
    An alternative is to use a multimedia timer if better accuracy is of great importance or considering other workarounds as CDrive linked to.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Timer Accuracy

    Okay, so the Timer control is not super accurate but something set to take 3 seconds taking 5 seconds is really inaccurate and suggests to me the your code takes longer than 100ms to run. Have your tried it when your application is compiled and have you tried an interval of 200ms just to see if it evens things up a bit. It might be that a timer is okay for your needs.

  8. #8
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Timer Accuracy

    The problem with the VB timer is that the interval does not set the time between the timer events, instead, it just tells the timer how much to wait until the next call....

    What I mean is, for example

    Say your interval is 1 second (1000 ms), and the code in the timer event takes 500 ms

    Then first call is after 1000 ms you enable it
    your code runs for 500 ms
    timer waits for 1000 ms
    At this point you have a total of 2500 ms (2.5 seconds) since you started the timer
    timer fires again
    your code runs for 500 ms
    timer waits for 1000 ms
    at this point 4 seconds have passed since you started the timer.

    So I hope you get the idea....

    What you have to do is something like this:

    Set timer interval at 1000 ms
    Timer event starts
    save time started
    run your code for whatever time (but less than 1000 ms)
    save time again, to calculate how long your code took, lets say it took 300 ms
    Reset the timer i.e. Timer.Enabled = False
    Set new interval: Timer.Interval = 1000 - 300
    Start the timer: Timer.Enable = True

    That way, the new interval will start after 2000 ms after you originally started it...

    Of course doing it like this is STILL not very precise, but still as close as you can get with VB timer.....

    If you want something a lot more precise, see my thread in the CodeBank here:
    VB6 - A very precise timer using Multithreading

    That timer will fire at interval set, regardless of how long your code takes in the event (as long as it's less than the interval). Even if your code takes more than the interval, the tiner will recover at next interval if your code is faster than the interval.

    It also returns a value for you to see if the timer is running behind (when your code takes longer than the interval)

    The precision is between 0.1 to 1 ms (depending on your computer, and how many programs run at the time...) but still it is far more precise than the VB timer at 15.625 ms
    Last edited by CVMichael; Sep 26th, 2008 at 07:28 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width