Results 1 to 12 of 12

Thread: timer control ? *RESOLVED*

  1. #1

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740

    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:
    1. 'Declare tCount
    2. Dim tCount As Integer
    3. Dim tCount2 As Integer
    4.  
    5. Private Function SecsToTime(Seconds As Integer) As String
    6. Dim Hours As Integer, Minutes As Integer
    7. 'Hours = Seconds \ 3600 'seconds in 1 hour
    8. 'Seconds = Seconds Mod 3600
    9. Minutes = Seconds \ 60 'seconds in 1 minute
    10. Seconds = Seconds Mod 60
    11. 'SecsToTime = Hours & "h: " & Format(Minutes, "00") & "m: " & Format(Seconds, "00.")
    12. SecsToTime = Hours & "m: " & Format(Seconds, "00.")
    13. End Function
    14. Private Function SecsToMill(Seconds As Integer) As String
    15. Seconds = Seconds Mod 60
    16. SecsToMill = Hours & Format(Seconds, "00") & "s:"
    17. End Function
    18.  
    19. Private Sub Command1_Click()
    20. Timer1.Enabled = False
    21. Timer2.Enabled = False
    22. End Sub
    23.  
    24. Private Sub Timer1_Timer()
    25. 'Increase the second count
    26. tCount = tCount + 1
    27. 'Me.Caption = tCount
    28. 'Get the formatted timing
    29. Label1.Caption = SecsToTime(tCount)
    30. End Sub
    31.  
    32. Private Sub Timer2_Timer()
    33. 'Increase the second count
    34. tCount2 = tCount2 + 1
    35. 'Me.Caption = tCount2
    36. 'Get the formatted timing
    37. Label2.Caption = SecsToMill(tCount2)
    38. End Sub
    39.  
    40. Private Sub Form_Load()
    41. 'Set the initial caption
    42. Label1.Caption = "00m: 00."
    43. Label2.Caption = "00s"
    44. 'Set the interval to ~1 second
    45. Timer1.Interval = 1000 'milliseconds = 1 second
    46. Timer2.Interval = 1 'milliseconds = 1/1000 second
    47. 'Reset the second counter
    48. tCount = 0
    49. End Sub
    Last edited by Navarone; Aug 8th, 2003 at 08:02 AM.
    He who never made a mistake never made a discovery?

  2. #2

  3. #3
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    416
    keep in mind- the timer control is only accurate to about 50ms.

  4. #4

  5. #5

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740
    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?

  6. #6

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740
    How would the GetTickCount work in this case?
    He who never made a mistake never made a discovery?

  7. #7

  8. #8

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740
    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:
    1. For lngCounterOne = 1 To 1000000
    2.         For lngCounterTwo = 1 To 5
    3.         Next lngCounterTwo
    4.     Next lngCounterOne
    He who never made a mistake never made a discovery?

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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:
    1. For lngCounterOne = 1 To 1000000
    2.         For lngCounterTwo = 1 To 5
    3.         Next lngCounterTwo
    4.     Next lngCounterOne
    That was just some code that I added to simulate some long running process.

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3.  
    4. Private Sub Command1_Click()
    5.  
    6.     Dim lngStart As Long
    7.     Dim lngNow As Long
    8.     Dim lngDiff As Long
    9.     Dim lngMilSec As Long
    10.     Dim lngSeconds As Long
    11.     Dim lngMinutes As Long
    12.    
    13.     ' Record the starting tick count
    14.     lngStart = GetTickCount()
    15.     Do
    16.         ' Get the current tick count. Note: The tick count is reset every 49.7 hours
    17.         lngNow = GetTickCount()
    18.         ' Determine the number of ticks since we started
    19.         lngDiff = lngNow - lngStart
    20.        
    21.         ' Do time calculations. 1000 ticks occur each second.
    22.         lngMinutes = lngDiff \ 60000
    23.         lngSeconds = (lngDiff \ 1000&) Mod 60&
    24.         lngMilSec = lngDiff Mod 1000&
    25.        
    26.         Label1.Caption = "Minutes: " & lngMinutes & _
    27.                        ", Seconds: " & lngSeconds & _
    28.                        ", ms: " & lngMilSec
    29.         DoEvents
    30.     Loop
    31. End Sub

  11. #11

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740
    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?

  12. #12
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    VB Code:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3. [color="#FF0080"]Private mbStop As Boolean[/color]
    4.  
    5. Private Sub Command1_Click()
    6.  
    7.     Dim lngStart As Long
    8.     Dim lngNow As Long
    9.     Dim lngDiff As Long
    10.     Dim lngMilSec As Long
    11.     Dim lngSeconds As Long
    12.     Dim lngMinutes As Long
    13.    
    14.    [color="#FF0080"] mbStop = False[/color]
    15.     ' Record the starting tick count
    16.     lngStart = GetTickCount()
    17.     Do
    18.         ' Get the current tick count. Note: The tick count is reset every 49.7 hours
    19.         lngNow = GetTickCount()
    20.         ' Determine the number of ticks since we started
    21.         lngDiff = lngNow - lngStart
    22.        
    23.         ' Do time calculations. 1000 ticks occur each second.
    24.         lngMinutes = lngDiff \ 60000
    25.         lngSeconds = (lngDiff \ 1000&) Mod 60&
    26.         lngMilSec = lngDiff Mod 1000&
    27.        
    28.         Label1.Caption = "Minutes: " & lngMinutes & _
    29.                        ", Seconds: " & lngSeconds & _
    30.                        ", ms: " & lngMilSec
    31.         DoEvents
    32.         [color="#FF0080"]If mbStop Then
    33.             Exit Do
    34.         End If[/color]    
    35.     Loop
    36. End Sub
    37.  
    38. [color="#FF0080"]Private Sub Command2_Click()
    39.    
    40.     mbStop = True
    41.    
    42. 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
  •  



Click Here to Expand Forum to Full Width