Results 1 to 9 of 9

Thread: Timer Control Interval

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    34

    Timer Control Interval

    It is known that when a timer control is "Enabled" it commences it's count as determined by the "Interval".

    The Timer is effectively disabled on entry of the timer-event handler; in that no further events will be triggered until the event processing ends (the handler is exited).
    If explicit disable/enable instructions are NOT embedded in the handler, does the timer start at the beginning of the interval on exiting the event handler?

    In other words if the timer interval is 50 ms and the event-handler takes 2 ms; IS the effective timer interval 52 ms; OR if not explicitly disabled on handler entry does the timer continue to tick at the OS level yielding an effective constant 50 ms interval period, independent of the event-handler 2 ms processing time?

    Perhaps a simpler way of stating the question; is there any difference in the effective timer interval by either including an explicit timer-disable on entry of the timer-event, and an enable-timer on exit of the event processing, verses not including them?

    Hope I haven't been too verbose, Mickey

    P.S. If I were to bet, I expect that the timer-event handler processing time must always be included in determining the effective timer interval.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Timer Control Interval

    If the interval is 50ms, then that is a guaranteed minimum of 50ms after you leave the event until the next tick, so yes if you take 10 ms to process, you would cycle at a 60ms in theory, but it will always be 60ms or more, never less, and doesn't adjust to make up time or try to give you 60ms on average.

    There is actually a clock signal that determines the minimum interval period, so the tick event will only happen on one of those clock ticks.
    Most of the time, I've found that lately (for over 10 years) to be a 64hz clock (unless some program has called an API to change the master clock interval used).
    That means the minimum interval is actually 15.625 ms, and whatever value you choose will result in a multiple of that value.
    For instance, if you pick an interval of 1 ms (1000 hz), the tick event will still happen at 64 hz, so you'll only run a maximum of 64 times per second.
    If you choose 10ms, then you still get 64 ticks per second.

    If you choose 20ms, then in theory should be getting ticks at 50hz, the reality will be that you will be running at 32hz.
    When you exit the event, you will get a tick on the next tick of the 64hz clock that occurs at least 20ms after you leave.
    So, the next 64hz tick will occur in that 20ms window (15.625 ms after the last master clock tick), then the 20ms time you asked for will have expired, then the next tick of the master clock (now 31.25 ms after the last tick event you received) you will receive another tick event.

    Thus, you ask for 50hz, you'll get 32hz.

    So, your true frequency of tick events at best with a 64 master clock, will be 64/1 hz, 64/2 hz, 64/3 hz, 64/4 hz, etc... (i.e. 64hz, 32hz, 21.333 hz, 16 hz, etc).
    In Interval values, 15.625, 31.25, 46.875, etc.

    Since the minimum interval the ticks can occur at is easily, but globally changed by an API call, you may find on different machines, the base rate of the clock won't be 64hz for your timer events. If you want to use the timer, and adjust as best you can for the frequency of the resulting ticks, you'll need to track how much actual time is passing between ticks and make adjustments.
    It is unfortunate that the base tick rate can vary from machine to machine, since it is modifiable by software, the rate may change over the course of the day if some software changes it.
    Last edited by passel; Jun 23rd, 2018 at 11:44 PM.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    34

    Re: Timer Control Interval

    "So, the next 64hz tick will occur in that 20ms window (15.625 ms after the last master clock tick), then the 20ms time you asked for will have expired, then the next tick of the master clock (now 31.25 ms after the last tick event you received) you will receive another tick event."

    Wow, passel, this is quite a revelation. Not, that I really doubted you, but I did confirm it with a little experiment - and confirm it I did!
    Again I don't understand why the MS VB designers would be so misleading; no reference to this in the VB help that I can find?

    You referenced an API call to affect the base-rate of the clock; I have a copy of Appleman's "Visual Basic Programmer's Guide to the Win32 API". Which specific API call are you referring to?

    Thanks, for you response, Mickey

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Timer Control Interval

    have a look
    https://blogs.msdn.microsoft.com/lar...he-timer-apis/
    maybe read all the comments too
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    34

    Re: Timer Control Interval

    Thanks for the response westconn1,

    I'm currently investigating "SetTimer" in-lieu of the Timer Control.

    Mickey

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Timer Control Interval

    Mickey,

    It would help if you outlined a bit more about what you're trying to do. I've wrestled with timer events on several occasions. Personally, I'm not sure SetTimer is going to buy you much beyond what you get with the timer control.

    When I need time precision, I typically build a loop. Otherwise, things can get really messy trying to figure out how to keep track of things.

    I've got a high-precision Timer function (not a control that generates events, but just a function to query elapsed time). Just as another FYI, the built-in VB6 Timer function has similar precision problems as the Timer control (and SetTimer API calls).

    Here's that code, as there's not much to it. However, I've given you ALL of it, so paste it into Notepad, save as a CLS (TimerEx.cls), and then include it into your project.

    Code:
    
    VERSION 1.0 CLASS
    BEGIN
      MultiUse = -1  'True
      Persistable = 0  'NotPersistable
      DataBindingBehavior = 0  'vbNone
      DataSourceBehavior  = 0  'vbNone
      MTSTransactionMode  = 0  'NotAnMTSObject
    END
    Attribute VB_Name = "TimerEx"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = True
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    '
    ' This class has VB_PredeclaredId = True.
    ' Also, the Value is set to the default property.
    ' Therefore, TimerEx will just act like a new built-in function.
    ' No need to declare an object variable or explicitly instantiate.
    '
    Option Explicit
    '
    Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
    Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
    '
    Private cFreq As Currency
    '
    
    Private Sub Class_Initialize()
        QueryPerformanceFrequency cFreq
    End Sub
    
    Public Function Value() As Double
    Attribute Value.VB_UserMemId = 0
        ' Must be Public (not Friend) so we can set default property.
        ' Returns seconds, a double, with high-precision.
        ' Caller is responsible for keeping track of start/stop times.
        ' Basically, it works exactly like the built-in Timer function, but with much more precision.
        Dim cValue As Currency
        QueryPerformanceCounter cValue
        Value = cValue / cFreq  ' The division will return a double, and it also cancels out the Currency decimal points.
    End Function
    
    
    Once included, you'll just have a new "Function" (actually an instantiated class with a default method) named TimerEx. You can use this to figure out elapsed time with high precision.

    Also, one last FYI, if you do wind up going with a loop option, you may want to read about the Sleep API call. You can call it with Sleep 0 to relieve a bit of the strain that a tight loop puts on your CPU.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    34

    Re: Timer Control Interval

    Elroy,

    Thanks I will look over the information you've provided. I can also confirm after testing SetTimer does not buy anything, as passel has found with the timer-control, SetTimer does the same.
    Further, westconn1 has provided information that seems to suggest that modifying the system timer to gain higher resolution is not a good idea, as it puts further load on the system.

    My interests here have become more an exercise in curiosity than necessity to increase the resolution of timed functions.

    Thanks again for your help,

    Mickey
    Last edited by MickeyXm; Jun 24th, 2018 at 06:29 PM.

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Timer Control Interval

    Mickey,

    Some have suggested that the MultiMedia timer API calls have increased resolution. I've studied them a bit, but I've never completely gotten them up and running in VB6. For some of my purposes, I also need high-precision timers. I don't necessarily need anything faster than a millisecond, but just more precision, say 10.25 milliseconds. I've never been able to accomplish this with events, and have always resorted to loops, using the TimerEx code I provided above.

    If you can get a high-precision timer event sorted (without some loop running in your code somewhere), that'd be a very cool thing.

    Take Care,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Timer Control Interval

    The API call I was referring to was the timeBeginPeriod function (Item 3 in westconn1's link).
    You use it as a pair, calling timeBeginPeriod to change the minimum resolution for periodic timers, and later need to call timeEndPeriod with the same value to release your request.

    The issue is that this is a global setting, and if multiple processes call the function, the minimum periodic time will be the smallest requested from any process.
    If you test the timer and find you have the 64hz cycle running and assume that is the rate you're going to be ticked at, and you've chosen a small value, e.g. 1ms for your interval to allow the shortest period of wait after the event, allowing you to use more of the time in the event without slipping a frame and then another process calls timeBeginPeriod to change the minimum resolution to 1, your cycle that you expect to be running at 64hz, may be running much faster than 64hz, (I've usually found it to be 500hz, i.e. a 2ms cycle, but it depends on the the hardware and how quick your processing is).

    And as you noted, if you change the minimum resolution, since it affects all processes and the OS itself, it can reduce overall system performance as more time is chewed up by the increase in task switching. I'm sure you can read more about it now that you know the name of the API function.
    Last edited by passel; Jun 24th, 2018 at 07:57 PM.

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