VBA中使用API函数SetTimer和KillTimer做定时器发生死机现象?
请高手帮忙看看定时器,为啥我在VBE中点击停止运行的按钮后,代码仍然不断的在循环,也就是说VBE调试窗口的停止按钮并没有真正的关掉程序;
这种情况经常会导致有该定时器的程序发生死机或因错误引起的工作簿的关闭。
在模块中的代码如下:
Private Declare Function SetTimer Lib "user32.dll" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32.dll" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Public lTimerID As Long
Private Const lDuration = 300
Public Sub Start_Timer()
If lTimerID = 0 Then
lTimerID = SetTimer(0&, 0&, lDuration, AddressOf ontime)
Else
Call Stop_Timer
lTimerID = SetTimer(0&, 0&, lDuration, AddressOf ontime)
End If
End Sub
Private Sub Stop_Timer()
KillTimer 0&, lTimerID
End Sub
Private Sub ontime()
Dim a, b, c
a = 1 + 2
End Sub
Private Sub UserForm_Initialize()
Call Start_Timer
End Sub