Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stopwatch Trouble

Status
Not open for further replies.

goatsaregreat

Programmer
Mar 21, 2001
82
GB
I need to create a stopwatch in the minutes:seconds.tenths,hundreths (00:00.00) format. I use a timer to control this, but the timer will not go fast enough to count hundreths of seconds. Someone suggested the following code to me, but it is the same result.

Option Explicit
'
' timer1.interval = 10
'

Dim lngTime As Long
Dim blStarted As Boolean

Private Sub Command1_Click()
lngTime = 0
blStarted = True
Text1 = "00:00:00"
End Sub

Private Sub Command2_Click()
blStarted = False
End Sub

Private Sub Timer1_Timer()
Dim lngMin As Long, lngSec As Long, lngHund As Long
If blStarted Then
lngTime = lngTime + 1
lngSec = Int(lngTime / 1000)
lngHund = lngTime Mod 100
lngMin = Int(lngSec / 600)
lngSec = lngSec Mod 60
Text1 = Format(lngMin, "00") & ":" & Format(lngSec, "00") & ":" & Format(lngHund, "00")
End If
End Sub


I have put the timer interval at "1" with no change. It still is not fast enough. TIA for your help.
 
Well, the code above is unfortunately fatally flawed - for several reasons. A couple of those reasons are that all timer events are actually posted to the message queue, so they are not guaranteed to raise an event in your application exactly on the specified interval nor that the event will be raised at exactly equal intervals, and that the underlying timer used for timer controls does not actually have a resolution down to 1 millisecond (even though you are allowed to set an anticipated interval that small); as I recall the actual resolution is somewhere about 18ms.

Try this alternative version (it still suffers from a certain amount of timing accuracy issues). I have tried to keep it as close to possible to your version:
[tt]
Option Explicit
'
' timer1.interval = 10
'

Dim lngTime As Long
Dim blStarted As Boolean
Dim StartTime As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long


Private Sub Command1_Click()
lngTime = GetTickCount '0
blStarted = True
Text1 = "00:00:00"
StartTime = GetTickCount
End Sub

Private Sub Command2_Click()
blStarted = False
End Sub

Private Sub Timer1_Timer()
Dim lngMin As Long, lngSec As Long, lngHund As Long
If blStarted Then
lngTime = GetTickCount - StartTime
lngSec = Int(lngTime / 1000)
lngHund = lngTime Mod 100
lngMin = Int(lngSec / 600)
lngSec = lngSec Mod 60
Text1 = Format(lngMin, "00") & ":" & Format(lngSec, "00") & ":" & Format(lngHund, "00")
End If
End Sub
[/tt]
If you want really accurate timers consider using the multimedia or high-resolution timers. The CCRP people package both of these up quite nicely in their timer package. See
 
It works great except when you time for more than 59.99 seconds. The minute numbers do not change from 00. They should also be counting. TIA for your continued help.
 
As I said, this is just a minor reworking of your code, which had several issues, of which the actual timer event resolution etc was only one area. Here, have a debugged version:
[tt]
Option Explicit
'
' timer1.interval = 10
'

Dim lngTime As Long
Dim blStarted As Boolean
Dim StartTime As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long


Private Sub Command1_Click()
lngTime = GetTickCount '0
blStarted = True
Text1 = "00:00:00"
StartTime = GetTickCount
End Sub

Private Sub Command2_Click()
blStarted = False
End Sub

Private Sub Timer1_Timer()
Dim lngMin As Long, lngSec As Long, lngHund As Long
If blStarted Then
lngTime = GetTickCount - StartTime
lngSec = lngTime \ 1000
lngHund = lngTime Mod 100
lngMin = lngSec \ 60
lngSec = lngSec Mod 60
Text1 = Format(lngMin, "00") & ":" & Format(lngSec, "00") & ":" & Format(lngHund, "00")
End If
End Sub
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top