|
-
Aug 6th, 2003, 12:30 PM
#1
Thread Starter
Fanatic Member
timer control ? *RESOLVED*
I have been messn around with the folowing code and was wondering if there might be a better way to do what I am attempting to achieve.
I two lables setup and two timers. The first timer counts in seconds and displays "00m 00." the second timer counts in milliseconds, and displays ".00s". The effect I am looking for is to see the milliseconds run.
VB Code:
'Declare tCount
Dim tCount As Integer
Dim tCount2 As Integer
Private Function SecsToTime(Seconds As Integer) As String
Dim Hours As Integer, Minutes As Integer
'Hours = Seconds \ 3600 'seconds in 1 hour
'Seconds = Seconds Mod 3600
Minutes = Seconds \ 60 'seconds in 1 minute
Seconds = Seconds Mod 60
'SecsToTime = Hours & "h: " & Format(Minutes, "00") & "m: " & Format(Seconds, "00.")
SecsToTime = Hours & "m: " & Format(Seconds, "00.")
End Function
Private Function SecsToMill(Seconds As Integer) As String
Seconds = Seconds Mod 60
SecsToMill = Hours & Format(Seconds, "00") & "s:"
End Function
Private Sub Command1_Click()
Timer1.Enabled = False
Timer2.Enabled = False
End Sub
Private Sub Timer1_Timer()
'Increase the second count
tCount = tCount + 1
'Me.Caption = tCount
'Get the formatted timing
Label1.Caption = SecsToTime(tCount)
End Sub
Private Sub Timer2_Timer()
'Increase the second count
tCount2 = tCount2 + 1
'Me.Caption = tCount2
'Get the formatted timing
Label2.Caption = SecsToMill(tCount2)
End Sub
Private Sub Form_Load()
'Set the initial caption
Label1.Caption = "00m: 00."
Label2.Caption = "00s"
'Set the interval to ~1 second
Timer1.Interval = 1000 'milliseconds = 1 second
Timer2.Interval = 1 'milliseconds = 1/1000 second
'Reset the second counter
tCount = 0
End Sub
Last edited by Navarone; Aug 8th, 2003 at 08:02 AM.
He who never made a mistake never made a discovery?
-
Aug 6th, 2003, 12:41 PM
#2
You don't need two timers. Just use Timer1 and each time the count gets to a multiple of 1000, add a second. You also don't need two labels. you can concatenate the two numbers in one label.
-
Aug 6th, 2003, 12:55 PM
#3
Hyperactive Member
keep in mind- the timer control is only accurate to about 50ms.
-
Aug 6th, 2003, 12:59 PM
#4
Yes, you might be better off using GetTickCount.
-
Aug 6th, 2003, 01:07 PM
#5
Thread Starter
Fanatic Member
Eras3r,
Yes , I know the timer is good to about 50ms, this however is realy just for show.
Martinliss,
I understand the part about concatenating the strings, but I am not sure about how the timer thing would work?
He who never made a mistake never made a discovery?
-
Aug 6th, 2003, 01:09 PM
#6
Thread Starter
Fanatic Member
How would the GetTickCount work in this case?
He who never made a mistake never made a discovery?
-
Aug 6th, 2003, 01:14 PM
#7
GetTickCount returns a time difference in milliseconds. See the Time processes using GetTickCount link in my signature.
-
Aug 6th, 2003, 01:34 PM
#8
Thread Starter
Fanatic Member
Ok, I see that. I just stuck the code in my sub formLoad to see what I could see and when I ran the code the message box showed "551", which I assume is how long it took for the form to open?
But I would want to start timing when I start recording and display that in a label and then stop timing when I stop recording. Can you explain a little about what this part of the code is doing and what the values mean?
VB Code:
For lngCounterOne = 1 To 1000000
For lngCounterTwo = 1 To 5
Next lngCounterTwo
Next lngCounterOne
He who never made a mistake never made a discovery?
-
Aug 6th, 2003, 01:46 PM
#9
Originally posted by Navarone
Ok, I see that. I just stuck the code in my sub formLoad to see what I could see and when I ran the code the message box showed "551", which I assume is how long it took for the form to open?
But I would want to start timing when I start recording and display that in a label and then stop timing when I stop recording. Can you explain a little about what this part of the code is doing and what the values mean?
VB Code:
For lngCounterOne = 1 To 1000000
For lngCounterTwo = 1 To 5
Next lngCounterTwo
Next lngCounterOne
That was just some code that I added to simulate some long running process.
-
Aug 6th, 2003, 05:15 PM
#10
Here is an example of a clock based on GetTickCount. Create a form with a label and a command button and then add this code.
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
Dim lngStart As Long
Dim lngNow As Long
Dim lngDiff As Long
Dim lngMilSec As Long
Dim lngSeconds As Long
Dim lngMinutes As Long
' Record the starting tick count
lngStart = GetTickCount()
Do
' Get the current tick count. Note: The tick count is reset every 49.7 hours
lngNow = GetTickCount()
' Determine the number of ticks since we started
lngDiff = lngNow - lngStart
' Do time calculations. 1000 ticks occur each second.
lngMinutes = lngDiff \ 60000
lngSeconds = (lngDiff \ 1000&) Mod 60&
lngMilSec = lngDiff Mod 1000&
Label1.Caption = "Minutes: " & lngMinutes & _
", Seconds: " & lngSeconds & _
", ms: " & lngMilSec
DoEvents
Loop
End Sub
-
Aug 7th, 2003, 06:53 AM
#11
Thread Starter
Fanatic Member
Thanks MartinLiss, this is cool
My vb book doesn't cover the getTickCount, so I'll go to msdn library and see what it says.
How to you stop this though?
He who never made a mistake never made a discovery?
-
Aug 7th, 2003, 09:15 AM
#12
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
[color="#FF0080"]Private mbStop As Boolean[/color]
Private Sub Command1_Click()
Dim lngStart As Long
Dim lngNow As Long
Dim lngDiff As Long
Dim lngMilSec As Long
Dim lngSeconds As Long
Dim lngMinutes As Long
[color="#FF0080"] mbStop = False[/color]
' Record the starting tick count
lngStart = GetTickCount()
Do
' Get the current tick count. Note: The tick count is reset every 49.7 hours
lngNow = GetTickCount()
' Determine the number of ticks since we started
lngDiff = lngNow - lngStart
' Do time calculations. 1000 ticks occur each second.
lngMinutes = lngDiff \ 60000
lngSeconds = (lngDiff \ 1000&) Mod 60&
lngMilSec = lngDiff Mod 1000&
Label1.Caption = "Minutes: " & lngMinutes & _
", Seconds: " & lngSeconds & _
", ms: " & lngMilSec
DoEvents
[color="#FF0080"]If mbStop Then
Exit Do
End If[/color]
Loop
End Sub
[color="#FF0080"]Private Sub Command2_Click()
mbStop = True
End Sub[/color]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|