After I set a timer with SetTimer (lib "user32"), it must be killed with KillTimer before ending the script, otherwise it causes weird behaviors like closing the intellisense menu every time the (ghost) timer fires.
However, some times when in an emergency situation I must stop the script via the "reset" button of the VBE and this causes the timer to remain unkilled, with the consequent weird behaviours.
Is there a way to automatically run a KillTimer when I press the "reset" button?
CodePudding user response:
The timer continues in the application, separate from any single VB Project. But event scripts must be part of a VB Project. If you press the reset button, I don't imagine that it will be able to trigger an event script in the project that was just reset.
Instead, you can use the timer's script to track the project session and check to see if the session has been reset.
Here's a simplified example, using Application.OnTime
instead of SetTimer
:
Dim eventTime As Date
Sub StartTimer()
timer start:=True
End Sub
Sub timer(Optional start As Boolean = False)
If eventTime = 0 And Not start Then Exit Sub
Debug.Print Now
eventTime = Now TimeValue("00:00:02")
Application.OnTime eventTime, "Module1.Timer"
End Sub
The OnTime
loop continues even if you press the Reset Button, but the reset button clears the value in eventTime
so the next time the timer script runs, eventTime = 0
is true and the OnTime
loop is exited.