Es können mehrere Timer verschiedene Aufgaben erfüllen.

Nachhfolgend ein Beispiel ohne WndProc, bzw. ohne Fenster/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


Wird lediglich eine Timerproc verwendet, so gibt es ohne Handle und TimerID keine Möglichkeit festzustellen, welcher Timer den Event ausgelöst hat.

Anstelle der Debug.Print-Anweisung kann natürlich jeder andere gültige Befehl stehen.

Der iCounter dient lediglich als Abbruch-Kriterium.

Zum Seitenanfang