How to make safe API Timers in VBA?

前端 未结 6 981
情话喂你
情话喂你 2020-12-01 21:46

I read in various places that API timers are risky in VBA, that if you edit a cell while the timer is running it will crash Excel.

This code from http://optionexplic

6条回答
  •  不知归路
    2020-12-01 22:36

    With the API timer, as soon as I set the time lapse too short, Excel would crash because it had not finished the previous timed task before the next was scheduled. This does not happen with ontime because you set ontime after finishing TimerProc.

    Maybe it is possible to kill the timer first thing in Timerproc, and set a new one just before finishing.

    You should be aware that Killtimer actually fails at times, leaving the timer alive and continuing to call the procedure forever. So a machine gun code with feedback control is necessary to be sure it is really dead.

    //pseudo code :
    start_kill = timer()
    While still_alive= (negative result of killtimer) do
    Still_ailve = KillTimer TimerID.
    If timer - start_kill > 10 then msgbox "not dead     find a bigger gun" Exit sub
    Wend
    

    Of course you need a time out to exit this loop.

提交回复
热议问题