|
-
Sep 10th, 2001, 08:08 AM
#1
Thread Starter
Addicted Member
Stopwatch
I've seen examples of stopwatches on planetsource code. They are all either two fast or too slow.....inaccurate. They all use the timer control and to what i've seen in the forums, they are evil...
Can anyone give me examples of stopwatches done in API or any other accurate methods? It only has to count by seconds, not milliseconds.
You can just suggest the method but hopefully an example can be included. Thanx
-
Sep 10th, 2001, 08:16 AM
#2
Member
When you say these stopwatches use timers, I assume you mean as the source of the time. Here is one way to do it with using the timer as a source, but to update the stopwatch instead.
Set your timer interval to like 50 or 100 ms...whatever you like.
Private Sub Timer1_Timer()
Label1.Caption = Timer
End Sub
Hope this helps.
Cedric
-
Sep 10th, 2001, 08:17 AM
#3
Member
oops...made spelling error.
Meant to say...
Here is one way to do it WITHOUT using the timer as a source
-
Sep 10th, 2001, 08:17 AM
#4
Fanatic Member
There in an API for it, but the easiest way would be to use the "timer", not timer control, value and do the math your self.
cmdstart_click
StartTime = Timer
end sub
cmdstop_click
msgbox Timer-StartTime
end sub
Kurt Simons
[I know I'm a hack but my clients don't!]
-
Sep 10th, 2001, 08:24 AM
#5
Thread Starter
Addicted Member
thanx but...
I wanted the most accurate method and i still do please:P thanx
i think i know how to use the timer but is there anything where it doesn't involve a timer? I need to to be fairly accurate.
kurtsimons ----> can you remember the API or find out?
thanx again
-
Sep 10th, 2001, 10:25 AM
#6
If you only need the time elapsed since a certain event use the 'GetTickCount' API-Function. It's very easy to handle and a good way of getting used to API-Functions.
in a module:
public Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long
In your form:
VB Code:
Option Explicit
Dim lngTickCount As Long
Private Sub Command1_Click()
lngTickCount = GetTickCount
End Sub
Private Sub Command2_Click()
lngTickCount = GetTickCount - lngTickCount
End Sub
The example obviously doesn't do anything useful. It measures how long it takes you to click command2 after you have clicked command1.
You will get the idea though as I hope.
regards,
Helger
-
Sep 10th, 2001, 11:01 AM
#7
Fanatic Member
Digital's code worked fine for me! Maybe this example based on his code is a bit clearer??
VB Code:
'In a module
Public Declare Function SetTimer Lib "user32" (ByVal Hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal Hwnd As Long, ByVal nIDEvent As Long) As Long
Public mHwnd As Long
Public StopHasBeenPressed As Boolean
Public Sub Set_Timer(ByVal ID As Long, ByVal Seconds As Integer)
Call SetTimer(mHwnd, ID, CLng(Seconds * 1000), AddressOf TimerProc)
End Sub
Public Sub Kill_Timer(ByVal ID As Long)
Call KillTimer(mHwnd, ID)
End Sub
Public Sub TimerProc(ByVal Hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
Kill_Timer idEvent
If Not StopHasBeenPressed Then
Form1.Label1.Caption = Time()
Call Set_Timer(mHwnd, 1)
End If
End Sub
'On Form1 with Label1, Command1, and Command2
Option Explicit
Private Sub Command1_Click()
StopHasBeenPressed = Not StopHasBeenPressed
If StopHasBeenPressed Then
Command1.Caption = "Start"
Else
Form_Load
End If
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Initialize()
Label1.Caption = Time()
Command2.Caption = "Quit"
End Sub
Private Sub Form_Load()
Command1.Caption = "Stop"
mHwnd = Form1.Hwnd
StopHasBeenPressed = False
Call Set_Timer(mHwnd, 1)
End Sub
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
|