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?
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?
Comment