请考虑以下代码
Timer1 .Enabled := False; Timer1.Interval : = 300; For I := 1 to NumberOfTimesNeed do Begin Timer1 .Enabled := False; // Timer1 .Enabled := True; // reset the timer to 0.30 seconds TakesToLong := False; DoSomethingThatTakesTime; // Application.ProcessMessages is called in the procedure If TakesToLong = True then TakeAction; End; procedure Timer1Timer(Sender: TObject); begin TakesToLong:= True; end;
题 :
当我禁用然后启用Timer1时
Timer1.Enabled := False; Timer1.Enabled := True;
这会重置计时器吗?
即它会在超时之前等待0.30秒.
是的,它会的.如果之前启用了计时器,则将Enabled设置为False将调用Windows API函数KillTimer().如果之前未启用计时器,则将Enabled设置为True将调用Windows API函数SetTimer().
这是一个标准的习惯用法,自Delphi 1时代以来一直在运作.
但是,我会以不同的方式实现您的代码:
Start := GetSystemTicks; DoSomethingThatTakesTime; Duration := GetSystemTicks - Start; if Duration > 300 then TakeAction;
这将在没有计时器的情况下工作,并且无需在长时间采用方法中调用ProcessMessages().GetSystemTicks()是我在库中的一个函数,它在Windows中调用timeGetTime(),并且对Kylix实现了不同的实现(不记得如何,我很久以前就清除了该代码).