当前位置:  开发笔记 > 开发工具 > 正文

计时器在德尔福

如何解决《计时器在德尔福》经验,为你挑选了1个好方法。

请考虑以下代码

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秒.



1> mghie..:

是的,它会的.如果之前启用了计时器,则将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实现了不同的实现(不记得如何,我很久以前就清除了该代码).

推荐阅读
云聪京初瑞子_617
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有