Timer Control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Bub

    #1

    Timer Control

    Is there an accurate way to create a "stopwatch" good to 1/10 of a second?
    I'm not sure if I should use the timer control, or some way to access the
    computer timer. I found the following site

    which has the following:
    [color=blue]
    > Want to really get down to instants? This tip from reader Phil Lenoir[/color]
    tells you how.[color=blue]
    > --------------------------------------------------------------------------[/color]
    ------[color=blue]
    > The only way you can access fractions of a second in VB is using a Timer,[/color]
    Right? Wrong.[color=blue]
    > Use this simple technique to access time down to the resolution of the PC[/color]
    timer interrupt. It[color=blue]
    > relies on the fact that Microsoft's internal time format is stored as a[/color]
    Double-precision number[color=blue]
    > representing the number of years since 1900.
    > This code will pause for the requested fraction of a second:
    >
    > Public Sub Pause(dblHowLon g As Double)
    > Dim dblPauseUntil As Double
    > dblPauseUntil = CDbl(Now) + dblHowLong
    > While cDbl(Now) < dblPauseUntil
    > DoEvents
    > Wend
    > End Sub[/color]

    I didn't use this directly, but I wanted to see the resolution ofCDbl(Now),
    so I tried
    the following:

    Private Sub tmrDispTime_Tim er()
    tmrDispTime.Int erval = 500
    fNum = fNum + 1
    txtNum = txtNum + CStr(fNum) + vbCrLf
    txtTime = txtTime + CStr(CDbl(Now)) + vbCrLf
    End Sub

    I expected this to show a different value every 500ms, but instead I
    received the following for fNum 1-17:
    38487.901840277 8
    38487.901840277 8
    38487.901851851 9
    38487.901851851 9
    38487.901863425 9
    38487.901863425 9
    38487.901875
    38487.901886574 1
    38487.901886574 1
    38487.901898148 1
    38487.901898148 1
    38487.901909722 2
    38487.901909722 2
    38487.901921296 3
    38487.901921296 3
    38487.901932870 4
    38487.901944444 4

    Note that the timer events should come about 2 per second, and that the
    output seems to only change
    approx every other number, i.e. every second.

    If I use simply the timer event, and not the clock, how can I expect to
    update a display exactly 10 times per
    second, over 15 minutes or more? If each timer event restarted the timer for
    100 ms, wouldn't there still be
    time lost between events?


  • Allcomp

    #2
    Re: Timer Control

    Hello,

    Personally, I use the windows API instead of a Timer

    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    I don't know if it is good or not, but I have been told that the timer
    can slow down the computer, so I always use sleep instead of a timer.

    You must just pay attention that the sleep will wait 0.1 sec for
    example, but if the program between the sleeps last 0.05 sec, you will
    have 0.15 seconds between 2 sleeps (the sleep and the following code) so
    you must have a fast code between the sleeps (or use multi thread or
    multi process)

    Marc Allard
    Allcomp

    William Bub wrote:[color=blue]
    > Is there an accurate way to create a "stopwatch" good to 1/10 of a second?
    > I'm not sure if I should use the timer control, or some way to access the
    > computer timer. I found the following site
    > http://searchvb.techtarget.com/tip/1...535495,00.html
    > which has the following:
    >
    >[color=green]
    >>Want to really get down to instants? This tip from reader Phil Lenoir[/color]
    >
    > tells you how.
    >[color=green]
    >>--------------------------------------------------------------------------[/color]
    >
    > ------
    >[color=green]
    >>The only way you can access fractions of a second in VB is using a Timer,[/color]
    >
    > Right? Wrong.
    >[color=green]
    >>Use this simple technique to access time down to the resolution of the PC[/color]
    >
    > timer interrupt. It
    >[color=green]
    >>relies on the fact that Microsoft's internal time format is stored as a[/color]
    >
    > Double-precision number
    >[color=green]
    >> representing the number of years since 1900.
    >>This code will pause for the requested fraction of a second:
    >>
    >>Public Sub Pause(dblHowLon g As Double)
    >>Dim dblPauseUntil As Double
    >>dblPauseUnt il = CDbl(Now) + dblHowLong
    >>While cDbl(Now) < dblPauseUntil
    >>DoEvents
    >>Wend
    >>End Sub[/color]
    >
    >
    > I didn't use this directly, but I wanted to see the resolution ofCDbl(Now),
    > so I tried
    > the following:
    >
    > Private Sub tmrDispTime_Tim er()
    > tmrDispTime.Int erval = 500
    > fNum = fNum + 1
    > txtNum = txtNum + CStr(fNum) + vbCrLf
    > txtTime = txtTime + CStr(CDbl(Now)) + vbCrLf
    > End Sub
    >
    > I expected this to show a different value every 500ms, but instead I
    > received the following for fNum 1-17:
    > 38487.901840277 8
    > 38487.901840277 8
    > 38487.901851851 9
    > 38487.901851851 9
    > 38487.901863425 9
    > 38487.901863425 9
    > 38487.901875
    > 38487.901886574 1
    > 38487.901886574 1
    > 38487.901898148 1
    > 38487.901898148 1
    > 38487.901909722 2
    > 38487.901909722 2
    > 38487.901921296 3
    > 38487.901921296 3
    > 38487.901932870 4
    > 38487.901944444 4
    >
    > Note that the timer events should come about 2 per second, and that the
    > output seems to only change
    > approx every other number, i.e. every second.
    >
    > If I use simply the timer event, and not the clock, how can I expect to
    > update a display exactly 10 times per
    > second, over 15 minutes or more? If each timer event restarted the timer for
    > 100 ms, wouldn't there still be
    > time lost between events?
    >
    >[/color]

    Comment

    • Steve Gerrard

      #3
      Re: Timer Control


      "William Bub" <fathafluff@hot mail.com> wrote in message
      news:P7Uhe.3568 3$ia6.31372@twi ster.nyroc.rr.c om...
      [color=blue]
      > Is there an accurate way to create a "stopwatch" good to 1/10 of a second?[/color]

      The basic idea for all timing is to use a timer to cause events to occur, but to
      read a clock to get the elapsed time.The example below uses a timer called
      Timer1, and also calls the Timer() function, so don't confuse the two. The
      Timer() call returns elapsed seconds, and is good to between 5 and 50 msecs,
      depending on the OS.

      You can also use the API call GetTickCount, or even QueryPerformanc eCounter for
      very precise timings.

      Option Explicit

      Private mStart As Single

      Private Sub Form_Load()
      Timer1.Enabled = False
      ' interval smaller than desired resolution
      Timer1.Interval = 20
      End Sub

      Private Sub Command1_Click( )
      If Timer1.Enabled Then
      Timer1.Enabled = False
      Me.Caption = Format(Timer() - mStart, "0.0")
      Else
      Timer1.Enabled = True
      mStart = Timer()
      End If
      End Sub

      Private Sub Timer1_Timer()
      Me.Caption = Format(Timer() - mStart, "0.0")
      End Sub



      Comment

      • SpeedBoy

        #4
        Re: Timer Control

        The standard Microsoft timer control is inherently inaccurate, even for
        timing in 1/10th of a second intervals.

        Mabry Software makes a high-resolution timer control (Hitime.ocx) that is
        remarkably accurate down to (basically) 1/1000th of a second. I've used it
        in numerous "critical" timing projects with terrific success. It's
        reasonably priced, and easy to implement. A fully-functional demo version is
        available at their website (www.mabry.com).

        Frank Lewis
        SpeedFoundry, Inc.

        "William Bub" <fathafluff@hot mail.com> wrote in message
        news:P7Uhe.3568 3$ia6.31372@twi ster.nyroc.rr.c om...[color=blue]
        > Is there an accurate way to create a "stopwatch" good to 1/10 of a second?
        > I'm not sure if I should use the timer control, or some way to access the
        > computer timer. I found the following site
        > http://searchvb.techtarget.com/tip/1...535495,00.html
        > which has the following:
        >[color=green]
        > > Want to really get down to instants? This tip from reader Phil Lenoir[/color]
        > tells you how.
        >
        > --------------------------------------------------------------------------
        > ------[color=green]
        > > The only way you can access fractions of a second in VB is using a[/color][/color]
        Timer,[color=blue]
        > Right? Wrong.[color=green]
        > > Use this simple technique to access time down to the resolution of the[/color][/color]
        PC[color=blue]
        > timer interrupt. It[color=green]
        > > relies on the fact that Microsoft's internal time format is stored as a[/color]
        > Double-precision number[color=green]
        > > representing the number of years since 1900.
        > > This code will pause for the requested fraction of a second:
        > >
        > > Public Sub Pause(dblHowLon g As Double)
        > > Dim dblPauseUntil As Double
        > > dblPauseUntil = CDbl(Now) + dblHowLong
        > > While cDbl(Now) < dblPauseUntil
        > > DoEvents
        > > Wend
        > > End Sub[/color]
        >
        > I didn't use this directly, but I wanted to see the resolution[/color]
        ofCDbl(Now),[color=blue]
        > so I tried
        > the following:
        >
        > Private Sub tmrDispTime_Tim er()
        > tmrDispTime.Int erval = 500
        > fNum = fNum + 1
        > txtNum = txtNum + CStr(fNum) + vbCrLf
        > txtTime = txtTime + CStr(CDbl(Now)) + vbCrLf
        > End Sub
        >
        > I expected this to show a different value every 500ms, but instead I
        > received the following for fNum 1-17:
        > 38487.901840277 8
        > 38487.901840277 8
        > 38487.901851851 9
        > 38487.901851851 9
        > 38487.901863425 9
        > 38487.901863425 9
        > 38487.901875
        > 38487.901886574 1
        > 38487.901886574 1
        > 38487.901898148 1
        > 38487.901898148 1
        > 38487.901909722 2
        > 38487.901909722 2
        > 38487.901921296 3
        > 38487.901921296 3
        > 38487.901932870 4
        > 38487.901944444 4
        >
        > Note that the timer events should come about 2 per second, and that the
        > output seems to only change
        > approx every other number, i.e. every second.
        >
        > If I use simply the timer event, and not the clock, how can I expect to
        > update a display exactly 10 times per
        > second, over 15 minutes or more? If each timer event restarted the timer[/color]
        for[color=blue]
        > 100 ms, wouldn't there still be
        > time lost between events?
        >
        >[/color]


        Comment

        • Tom

          #5
          Re: Timer Control

          If you are running Windoze Millenium, the timer control is
          even slower.
          I wrote an animated app at work (NT machine) and it
          was moving quite well. I brought it home and tried it
          with Millenium. It was so slow that I got bored watching.
          At tht time, I had a dual boot machine. A reboot to Win2K
          pro produced no problem.
          I agree with the others, API calls are best for accurate timing.

          Tom.


          "William Bub" <fathafluff@hot mail.com> wrote in message
          news:P7Uhe.3568 3$ia6.31372@twi ster.nyroc.rr.c om...[color=blue]
          > Is there an accurate way to create a "stopwatch" good to 1/10 of a second?
          > I'm not sure if I should use the timer control, or some way to access the
          > computer timer. I found the following site
          > http://searchvb.techtarget.com/tip/1...535495,00.html
          > which has the following:
          >[color=green]
          > > Want to really get down to instants? This tip from reader Phil Lenoir[/color]
          > tells you how.
          >
          > --------------------------------------------------------------------------
          > ------[color=green]
          > > The only way you can access fractions of a second in VB is using a[/color][/color]
          Timer,[color=blue]
          > Right? Wrong.[color=green]
          > > Use this simple technique to access time down to the resolution of the[/color][/color]
          PC[color=blue]
          > timer interrupt. It[color=green]
          > > relies on the fact that Microsoft's internal time format is stored as a[/color]
          > Double-precision number[color=green]
          > > representing the number of years since 1900.
          > > This code will pause for the requested fraction of a second:
          > >
          > > Public Sub Pause(dblHowLon g As Double)
          > > Dim dblPauseUntil As Double
          > > dblPauseUntil = CDbl(Now) + dblHowLong
          > > While cDbl(Now) < dblPauseUntil
          > > DoEvents
          > > Wend
          > > End Sub[/color]
          >
          > I didn't use this directly, but I wanted to see the resolution[/color]
          ofCDbl(Now),[color=blue]
          > so I tried
          > the following:
          >
          > Private Sub tmrDispTime_Tim er()
          > tmrDispTime.Int erval = 500
          > fNum = fNum + 1
          > txtNum = txtNum + CStr(fNum) + vbCrLf
          > txtTime = txtTime + CStr(CDbl(Now)) + vbCrLf
          > End Sub
          >
          > I expected this to show a different value every 500ms, but instead I
          > received the following for fNum 1-17:
          > 38487.901840277 8
          > 38487.901840277 8
          > 38487.901851851 9
          > 38487.901851851 9
          > 38487.901863425 9
          > 38487.901863425 9
          > 38487.901875
          > 38487.901886574 1
          > 38487.901886574 1
          > 38487.901898148 1
          > 38487.901898148 1
          > 38487.901909722 2
          > 38487.901909722 2
          > 38487.901921296 3
          > 38487.901921296 3
          > 38487.901932870 4
          > 38487.901944444 4
          >
          > Note that the timer events should come about 2 per second, and that the
          > output seems to only change
          > approx every other number, i.e. every second.
          >
          > If I use simply the timer event, and not the clock, how can I expect to
          > update a display exactly 10 times per
          > second, over 15 minutes or more? If each timer event restarted the timer[/color]
          for[color=blue]
          > 100 ms, wouldn't there still be
          > time lost between events?
          >
          >[/color]


          Comment

          Working...