home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 3_2004-2005.ISO / Data / Zips / BAS_Module18049510142004.psc / ModuleMania / APITimer.bas < prev    next >
Encoding:
BASIC Source File  |  2004-04-28  |  5.7 KB  |  111 lines

  1. Attribute VB_Name = "APITimer"
  2. Option Explicit                                              '-⌐Rd-
  3.  
  4. ' =====================================================================
  5. ' WARNING!!! Code-only timers are inherently dangerous in the VB IDE,
  6. ' because the system blindly calls back into your code until the timer
  7. ' is turned off with an API KillTimer call.
  8. ' =====================================================================
  9.  
  10. ' System Timer API's
  11. Public Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapseMilliseconds As Long, ByVal lpTimerFunc As Long) As Long
  12. Public Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
  13.  
  14. ' Performance Counter API's
  15. Public Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
  16. Public Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
  17.  
  18. Private mCurFreq As Currency
  19.  
  20. ' =====================================================================
  21. ' Simple (imprecise) Timer - BeginTimer, EndTimer and TimerProc
  22. ' =====================================================================
  23. '
  24. ' BeginTimer creates a new timer, and returns the new timer's ID.
  25. ' This function returns zero on failure; no error is raised.
  26. '
  27. ' After the specified time has elapsed, the system calls the TimerProc
  28. ' sub-routine. You must add code to TimerProc to handle this event.
  29. '
  30. ' Each running timer calls the same TimerProc sub-routine, and so
  31. ' each TimerProc event is identified by the idEvent parameter which
  32. ' matches the ID returned by BeginTimer.
  33. '
  34. ' You shouldnÆt rely on the timer to be extremely accurate, as youÆre
  35. ' subject to the unpredictability of Win32 thread scheduling.
  36. '
  37. ' Remember - do not End or enter Debug mode while a timer is running!
  38. ' =====================================================================
  39. Public Function BeginTimer(ByVal lMilliseconds As Long) As Long
  40. Attribute BeginTimer.VB_Description = "BeginTimer creates a new timer, and returns the new timer's ID. This function returns zero on failure; no error is raised."
  41.     BeginTimer = SetTimer(0&, 0&, lMilliseconds, AddressOf TimerProc)
  42. End Function
  43.  
  44. ' =====================================================================
  45. ' EndTimer terminates the specified timer created by BeginTimer.
  46. ' The TimerID is passed ByRef and is reset to zero on success.
  47. '
  48. ' This function returns True on success, or False otherwise.
  49. ' =====================================================================
  50. Public Function EndTimer(ByRef TimerID As Long) As Boolean
  51. Attribute EndTimer.VB_Description = "EndTimer terminates the specified timer created by BeginTimer. The TimerID is passed ByRef and is reset to zero on success."
  52.     If (TimerID) Then EndTimer = KillTimer(0&, TimerID)
  53.     If EndTimer Then TimerID = 0 ' Reset TimerID
  54. End Function
  55.  
  56. ' =====================================================================
  57. ' After the specified time has elapsed, the system calls the TimerProc
  58. ' sub-routine. You must add code to TimerProc to handle this event.
  59. '
  60. ' Each running timer calls this same TimerProc sub-routine, and so
  61. ' each TimerProc event is identified by the idEvent parameter which
  62. ' matches the ID returned by BeginTimer.
  63. ' =====================================================================
  64. Private Sub TimerProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
  65. '    Static fInTimerProc As Boolean
  66. '    If fInTimerProc Then Exit Sub
  67. '    fInTimerProc = True
  68.  
  69.  
  70. ' hWnd    - Handle of CWnd that called SetTimer (0&)
  71. ' uMsg    - WM_TIMER = &H113
  72. ' idEvent - Timer identification (returned by BeginTimer)
  73. ' dwTime  - System time
  74. Debug.Print "The timer with ID " & idEvent & " just fired. System time is " & dwTime
  75.  
  76.  
  77. '    fInTimerProc = False
  78. End Sub
  79.  
  80. ' ========================================================================
  81. ' Precise Timer - ProfileStart and ProfileStop
  82. ' ========================================================================
  83. '
  84. ' ProfileStart returns the current value of the high-resolution performance
  85. ' counter as a Currency data type. You pass this value to ProfileStop which
  86. ' subtracts it from an ending count and returns the difference (elapsed time).
  87. '
  88. ' The result comes out nicely as a fixed-point Currency number representing
  89. ' seconds accurate to four decimal places. In the case of no high-resolution
  90. ' timer, these functions return zero.
  91. '
  92. ' Multiply by 1000 to convert to milliseconds (or 1000000 for microseconds).
  93. '
  94. ' Note - these functions return values too accurate to fit in a Long.
  95. ' ========================================================================
  96. Public Function ProfileStart() As Currency
  97. Attribute ProfileStart.VB_Description = "ProfileStart returns the current value of the high-resolution performance counter as a Currency data type. You pass this value to ProfileStop which subtracts it from an ending count and returns the difference."
  98.     If mCurFreq = 0 Then QueryPerformanceFrequency mCurFreq
  99.     If (mCurFreq <> 0) Then QueryPerformanceCounter ProfileStart
  100. End Function
  101.  
  102. Public Function ProfileStop(ByVal curStart As Currency) As Currency
  103. Attribute ProfileStop.VB_Description = "ProfileStart returns the current value of the high-resolution performance counter as a Currency data type. You pass this value to ProfileStop which subtracts it from an ending count and returns the difference."
  104.     If (mCurFreq <> 0) Then
  105.         Dim curStop As Currency
  106.         QueryPerformanceCounter curStop
  107.         ProfileStop = (curStop - curStart) / mCurFreq
  108.     End If
  109. End Function
  110. '                                                                    :¢)
  111.