Visual Basic > API and Miscellaneous
Create a timer using API calls
Create a timer using API calls The following code can be used instead of a the VB timer control, to create a routine which is continuous called at a specified time interval: Option Explicit Private Declare Function SetTimer Lib "user32" (ByVal Hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long Private Declare Function KillTimer Lib "user32" (ByVal Hwnd As Long, ByVal nIDEvent As Long) As Long Private lTimerId As Long 'Purpose : Stops the timer routine 'Inputs : N/A 'Outputs : Returns True if the timer routine was stopped 'Notes : 'Revisions : Function EndTimer() As Boolean If lTimerId Then lTimerId = KillTimer(0&, lTimerId) lTimerId = 0 EndTimer = True End If End Function 'Purpose : Starts the continuous calling of a private routine at a specific time interval. 'Inputs : lInterval The interval (in ms) at which to call the routine 'Outputs : N/A 'Notes : 'Revisions : Sub StartTimer(lInterval As Long) If lTimerId Then 'End Current Timer EndTimer End If lTimerId = SetTimer(0&, 0&, ByVal lInterval, AddressOf TimerRoutine) End Sub 'Purpose : Routine which is called repeatedly by the timer API. 'Inputs : Inputs are automatically generated. 'Outputs : 'Notes : 'Revisions : Private Sub TimerRoutine(ByVal lHwnd As Long, ByVal lMsg As Long, ByVal lIDEvent As Long, ByVal lTime As Long) 'Place your code here... End Sub
Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows