Results 1 to 1 of 1

Thread: Custom Timing Functions (Gettickcount)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Custom Timing Functions (Gettickcount)

    I made some timer functions that basically just count seconds..
    One function (TimerCount) will keep adding up and update quickly. But must be called in a timer to do so (Interval set to 1 works great).

    The other functions TimerStart and TimerEnd will be for seeing how long it takes certain code to be executed... (Helpful to find the fastest way to execute chunks of code)
    Module:
    vb Code:
    1. Option Explicit
    2.  
    3. Dim i As Single
    4. Dim Start As Long
    5. Dim EndT As Long
    6. Dim Total As Double
    7. Dim TotalLng As Long
    8.  
    9. Dim tmrStart As Long
    10. Dim tmrEnd As Long
    11. Dim tmrTtl As Double
    12.    
    13. Declare Function GetTickCount Lib "kernel32" () As Long
    14.  
    15. 'following function with milliseconds
    16. Public Function TimerCount(Optional Tmr As Timer, _
    17. Optional Obj As Object, Optional StopT As Double, _
    18. Optional AllObjwCap As Boolean, Optional LongT As Boolean) As Double
    19.    
    20.     Dim vCtrl As Control
    21.    
    22.     If Not IsMissing(LongT) Then 'for no milliseconds
    23.         If LongT = True Then
    24.             i = i + 1 ' for starting
    25.        
    26.             If i = 1 Then 'if its the first time running
    27.                 Start = GetTickCount 'get starting time
    28.             ElseIf i > 1 Then 'to keep the number low
    29.                 i = 2
    30.             End If
    31.        
    32.             EndT = GetTickCount 'get next time
    33.             TotalLng = (EndT - Start) / 1000 'translate into seconds
    34.             TimerCount = TotalLng 'function = time
    35.    
    36.             If Not IsMissing(Tmr) Then
    37.                 If StopT > 0 Then
    38.                     If StopT = TimerCount Then
    39.                         Tmr.Enabled = False
    40.                     End If
    41.                 End If
    42.             End If
    43.    
    44.             On Error Resume Next
    45.             If Obj <> vbNullString Then
    46.        
    47.                 If AllObjwCap = True Then
    48.                     For Each vCtrl In Obj
    49.                         vCtrl.Caption = TotalLng
    50.                         vCtrl.Text = TotalLng
    51.                     Next
    52.                 End If
    53.        
    54.             Obj.Text = TotalLng
    55.             Obj.Caption = TotalLng
    56.             If Err Then Err.Clear
    57.             End If
    58.            
    59.             Else
    60.            
    61.             i = i + 1 ' for starting
    62.        
    63.             If i = 1 Then 'if its the first time running
    64.                 Start = GetTickCount 'get starting time
    65.             ElseIf i > 1 Then 'to keep the number low
    66.                 i = 2
    67.             End If
    68.        
    69.             EndT = GetTickCount 'get next time
    70.             Total = (EndT - Start) / 1000 'translate into seconds
    71.             TimerCount = Total 'function = time
    72.    
    73.             If Not IsMissing(Tmr) Then 'if tmr has value
    74.                 If StopT > 0 Then ' if stoptime is greater than 0
    75.                     If StopT = TimerCount Then 'if stopt value = timerup value
    76.                         Tmr.Enabled = False 'disable timer user entered
    77.                     End If
    78.                 End If
    79.             End If
    80.    
    81.             On Error Resume Next
    82.             If Obj <> vbNullString Then 'if obj has value..
    83.        
    84.                 If AllObjwCap = True Then
    85.                     For Each vCtrl In Obj 'set all objects in objects caption
    86.                         vCtrl.Caption = Total
    87.                         vCtrl.Text = Total
    88.                     Next
    89.                 End If
    90.            
    91.             Obj.Text = Total
    92.             Obj.Caption = Total 'set obj's caption
    93.             If Err Then Err.Clear
    94.             End If
    95.    
    96.         End If
    97.     End If
    98.  
    99. 'Following function for without Milliseconds
    100.  
    101.    
    102. End Function
    103.  
    104. 'start the time todo function (amount of time to execute code)
    105. Public Function TimerStart()
    106.     tmrStart = GetTickCount
    107. End Function
    108.  
    109. 'end the time todo function (amount of time to execute code)
    110. Public Function TimerEnd(Optional Obj As Object, Optional LongTF As Boolean) As Double
    111.     tmrEnd = GetTickCount
    112.    
    113.     If tmrStart = 0 Then
    114.         Exit Function
    115.     End If
    116.    
    117.     If Not IsMissing(LongTF) Then
    118.         If LongTF = True Then
    119.             tmrTtl = (tmrEnd - tmrStart) \ 1000 'backslash division = rounding
    120.             tmrEnd = tmrTtl
    121.         Else
    122.             tmrTtl = (tmrEnd - tmrStart) / 1000
    123.             TimerEnd = tmrTtl
    124.         End If
    125.     End If
    126.    
    127.     On Error Resume Next
    128.     If Not IsMissing(Obj) Then
    129.         Obj.Caption = tmrTtl
    130.         Obj.Text = tmrTtl
    131.     End If
    132. End Function
    133.  
    134. Public Function TimerReset()
    135.     i = 0 'set so startt will set itself
    136. End Function

    Examples:
    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Call TimerReset 'resets the live timer
    5.     Timer1.Enabled = True 'not needed
    6.     'timer.enabled is optional, it starts the counting again
    7. End Sub
    8.  
    9. Private Sub Command2_Click()
    10.     '**** not Live counting
    11.     Call TimerStart 'start counting
    12.     'code to execute:
    13.     MsgBox "Sup home skillet"
    14.    
    15.     '...
    16.     '...
    17.    
    18.     Label1.Caption = TimerEnd(Nothing, False)  'end counting and show how long it took
    19.     'for the code to be executed
    20.    
    21.     'Another Example to use TimerEnd function:
    22.    
    23.     Call TimerEnd(Label1, False)
    24.     'label1 is any object supporting .caption or .text
    25.    
    26.     'false is milliseconds or not
    27. End Sub
    28.  
    29. Private Sub Timer1_Timer()
    30.     '**** Live counting (must be in a timer)
    31.    
    32.     Call TimerCount(Timer1, Text1, 0, False, False)
    33.    
    34.     'timer1 is the timer to be disabled
    35.     'text1 is the textbox to show the number of seconds
    36.     'text1 can be anything that supports .caption and .text
    37.    
    38.     '0 is number of seconds to disable timer at
    39.    
    40.     'false is to add every control in form
    41.     'to the number of seconds, supports .caption and .text
    42.    
    43.     'second false is if you want milliseconds or not
    44.    
    45.     'other example to use:
    46.    
    47.     Text1.Text = TimerCount(Timer1, Nothing, 0, False, False)
    48.    
    49. End Sub
    Last edited by bluehairman; Mar 23rd, 2008 at 07:40 PM.

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