Results 1 to 7 of 7

Thread: Stopwatch

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2001
    Posts
    162

    Cool 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

  2. #2
    Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    63
    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

  3. #3
    Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    63
    oops...made spelling error.
    Meant to say...
    Here is one way to do it WITHOUT using the timer as a source

  4. #4
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833
    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!]

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2001
    Posts
    162

    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

  6. #6
    Helger
    Guest
    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:
    1. Option Explicit
    2. Dim lngTickCount As Long
    3.  
    4. Private Sub Command1_Click()
    5. lngTickCount = GetTickCount
    6. End Sub
    7.  
    8. Private Sub Command2_Click()
    9. lngTickCount = GetTickCount - lngTickCount
    10. 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

  7. #7
    Fanatic Member
    Join Date
    Jul 2001
    Location
    London UK
    Posts
    671
    Digital's code worked fine for me! Maybe this example based on his code is a bit clearer??

    VB Code:
    1. 'In a module
    2. Public Declare Function SetTimer Lib "user32" (ByVal Hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    3. Public Declare Function KillTimer Lib "user32" (ByVal Hwnd As Long, ByVal nIDEvent As Long) As Long
    4. Public mHwnd As Long
    5. Public StopHasBeenPressed As Boolean
    6.  
    7. Public Sub Set_Timer(ByVal ID As Long, ByVal Seconds As Integer)
    8.     Call SetTimer(mHwnd, ID, CLng(Seconds * 1000), AddressOf TimerProc)
    9. End Sub
    10.  
    11. Public Sub Kill_Timer(ByVal ID As Long)
    12.     Call KillTimer(mHwnd, ID)
    13. End Sub
    14.  
    15. Public Sub TimerProc(ByVal Hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
    16. Kill_Timer idEvent
    17. If Not StopHasBeenPressed Then
    18. Form1.Label1.Caption = Time()
    19. Call Set_Timer(mHwnd, 1)
    20. End If
    21. End Sub
    22.  
    23.  
    24.  
    25.  
    26. 'On Form1 with Label1, Command1, and Command2
    27. Option Explicit
    28. Private Sub Command1_Click()
    29. StopHasBeenPressed = Not StopHasBeenPressed
    30. If StopHasBeenPressed Then
    31. Command1.Caption = "Start"
    32. Else
    33. Form_Load
    34. End If
    35. End Sub
    36.  
    37. Private Sub Command2_Click()
    38. Unload Me
    39. End Sub
    40.  
    41. Private Sub Form_Initialize()
    42. Label1.Caption = Time()
    43. Command2.Caption = "Quit"
    44. End Sub
    45.  
    46. Private Sub Form_Load()
    47. Command1.Caption = "Stop"
    48.     mHwnd = Form1.Hwnd
    49.     StopHasBeenPressed = False
    50. Call Set_Timer(mHwnd, 1)
    51. 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
  •  



Click Here to Expand Forum to Full Width