By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,647 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,647 IT Pros & Developers. It's quick & easy.

Timer Control

P: n/a
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:
Want to really get down to instants? This tip from reader Phil Lenoir tells you how. -------------------------------------------------------------------------- ------ The only way you can access fractions of a second in VB is using a Timer, Right? Wrong. Use this simple technique to access time down to the resolution of the PC timer interrupt. It relies on the fact that Microsoft's internal time format is stored as a Double-precision number representing the number of years since 1900.
This code will pause for the requested fraction of a second:

Public Sub Pause(dblHowLong As Double)
Dim dblPauseUntil As Double
dblPauseUntil = CDbl(Now) + dblHowLong
While cDbl(Now) < dblPauseUntil
DoEvents
Wend
End Sub


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

Private Sub tmrDispTime_Timer()
tmrDispTime.Interval = 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.9018402778
38487.9018402778
38487.9018518519
38487.9018518519
38487.9018634259
38487.9018634259
38487.901875
38487.9018865741
38487.9018865741
38487.9018981481
38487.9018981481
38487.9019097222
38487.9019097222
38487.9019212963
38487.9019212963
38487.9019328704
38487.9019444444

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?
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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:
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:

Want to really get down to instants? This tip from reader Phil Lenoir


tells you how.
--------------------------------------------------------------------------


------
The only way you can access fractions of a second in VB is using a Timer,


Right? Wrong.
Use this simple technique to access time down to the resolution of the PC


timer interrupt. It
relies on the fact that Microsoft's internal time format is stored as a


Double-precision number
representing the number of years since 1900.
This code will pause for the requested fraction of a second:

Public Sub Pause(dblHowLong As Double)
Dim dblPauseUntil As Double
dblPauseUntil = CDbl(Now) + dblHowLong
While cDbl(Now) < dblPauseUntil
DoEvents
Wend
End Sub

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

Private Sub tmrDispTime_Timer()
tmrDispTime.Interval = 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.9018402778
38487.9018402778
38487.9018518519
38487.9018518519
38487.9018634259
38487.9018634259
38487.901875
38487.9018865741
38487.9018865741
38487.9018981481
38487.9018981481
38487.9019097222
38487.9019097222
38487.9019212963
38487.9019212963
38487.9019328704
38487.9019444444

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?

Jul 17 '05 #2

P: n/a

"William Bub" <fa********@hotmail.com> wrote in message
news:P7*******************@twister.nyroc.rr.com...
Is there an accurate way to create a "stopwatch" good to 1/10 of a second?


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 QueryPerformanceCounter 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

Jul 17 '05 #3

P: n/a
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" <fa********@hotmail.com> wrote in message
news:P7*******************@twister.nyroc.rr.com...
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:
Want to really get down to instants? This tip from reader Phil Lenoir tells you how.

--------------------------------------------------------------------------
------
The only way you can access fractions of a second in VB is using a

Timer, Right? Wrong.
Use this simple technique to access time down to the resolution of the
PC timer interrupt. It
relies on the fact that Microsoft's internal time format is stored as a Double-precision number
representing the number of years since 1900.
This code will pause for the requested fraction of a second:

Public Sub Pause(dblHowLong As Double)
Dim dblPauseUntil As Double
dblPauseUntil = CDbl(Now) + dblHowLong
While cDbl(Now) < dblPauseUntil
DoEvents
Wend
End Sub


I didn't use this directly, but I wanted to see the resolution

ofCDbl(Now), so I tried
the following:

Private Sub tmrDispTime_Timer()
tmrDispTime.Interval = 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.9018402778
38487.9018402778
38487.9018518519
38487.9018518519
38487.9018634259
38487.9018634259
38487.901875
38487.9018865741
38487.9018865741
38487.9018981481
38487.9018981481
38487.9019097222
38487.9019097222
38487.9019212963
38487.9019212963
38487.9019328704
38487.9019444444

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?

Jul 17 '05 #4

P: n/a
Tom
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" <fa********@hotmail.com> wrote in message
news:P7*******************@twister.nyroc.rr.com...
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:
Want to really get down to instants? This tip from reader Phil Lenoir tells you how.

--------------------------------------------------------------------------
------
The only way you can access fractions of a second in VB is using a

Timer, Right? Wrong.
Use this simple technique to access time down to the resolution of the
PC timer interrupt. It
relies on the fact that Microsoft's internal time format is stored as a Double-precision number
representing the number of years since 1900.
This code will pause for the requested fraction of a second:

Public Sub Pause(dblHowLong As Double)
Dim dblPauseUntil As Double
dblPauseUntil = CDbl(Now) + dblHowLong
While cDbl(Now) < dblPauseUntil
DoEvents
Wend
End Sub


I didn't use this directly, but I wanted to see the resolution

ofCDbl(Now), so I tried
the following:

Private Sub tmrDispTime_Timer()
tmrDispTime.Interval = 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.9018402778
38487.9018402778
38487.9018518519
38487.9018518519
38487.9018634259
38487.9018634259
38487.901875
38487.9018865741
38487.9018865741
38487.9018981481
38487.9018981481
38487.9019097222
38487.9019097222
38487.9019212963
38487.9019212963
38487.9019328704
38487.9019444444

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?

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.