At first: Your code is wrong, please do as follows:
Copy the example below into a normal module
Open the immediate window
Execute sub StartTimer
Look a few seconds into the immediate window to see whats happen.
Execute sub CallTimer a few times to see whats happen.
Execute sub StopTimer
Also refers to the MSDN article for more information about SetTimer:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906%28v=vs.85%29.aspx
Now to your questions:
- IMHO no, except that your code need some time to execute.
- No / yes. Windows terminates the timer if the thread that creates the timer is closed.
- The timer is called automatically until you call KillTimer (or you compile your project during
development).
- Doesn't matter... for the timer handling.
- AFAIK no.
- If the timer code is called it might be possible that other code or events are interrupted temporary.
- No sure, but IMHO the code should run.
Andreas.
Option Explicit
Private hWnd As Long
Private Const WM_TIMER = &H113
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 Declare Function GetForegroundWindow Lib "user32" () As Long
Private Function TimerFunc(ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal nIDEvent As Long, ByVal dwTime As Long) As Long
'Called from the windows timer?
If wMsg = WM_TIMER Then
Debug.Print "Windows " & Now & ": " & hWnd & "," & nIDEvent
Else
Debug.Print "OurSelf " & Now & ": " & hWnd & "," & wMsg & "," & nIDEvent
End If
End Function
Sub StartTimer()
hWnd = GetForegroundWindow
SetTimer hWnd, 1, 1000, AddressOf TimerFunc
SetTimer hWnd, 2, 1500, AddressOf TimerFunc
End Sub
Sub CallTimer()
TimerFunc 123456, 0, 3, Now
End Sub
Sub StopTimer()
KillTimer hWnd, 1
KillTimer hWnd, 2
End Sub