Several timers can trigger different tasks.

An example without WndProc or  window handle:


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 iTimer1 As Integer                              'the timer identifier
Private iTimer2 As Integer                              'the timer identifier
Private Const lDelay1 As Long = 5000                    'the time in ms between WM_TIMER events
Private Const lDelay2 As Long = 3000                    'the time in ms between WM_TIMER events

Private Function TimerProc2(ByVal hwnd As Long, ByVal uMsg As Long, ByVal iTimerID As Long, ByVal dwTime As Long) As Long
   Static icounter As Integer
      
   Debug.Print Time, iTimerID, icounter
   icounter = icounter + 1
   If icounter > 5 Then
      KillTimer 0&, iTimer2                                              'stop timer
   End If
End Function


Private Function TimerProc1(ByVal hwnd As Long, ByVal uMsg As Long, ByVal iTimerID As Long, ByVal dwTime As Long) As Long
   Static icounter As Integer
      
   Debug.Print Time, iTimerID, icounter
   icounter = icounter + 1
   If icounter > 5 Then
      KillTimer 0&, iTimer1                                              'stop timer
   End If
End Function

Sub CATMain()
   
   Debug.Print Time
   
   iTimer1 = SetTimer(0&, 0&, lDelay1, AddressOf TimerProc1)                                                                    'start the timer
   iTimer2 = SetTimer(0&, 0&, lDelay2, AddressOf TimerProc2)                                                                    'start the timer

End Sub


Using a 'Timerproc', there is no way to tell, which timer triggered the event. Therefore each timer has it's own Timerproc.

Any other valid piece of code could be executed instead of  the 'Debug.Print' statements.

When the variable 'iCounter' equals 5 the timer is stopped.

Go to top