计时器在德尔福
发布时间:2020-12-15 09:44:05 所属栏目:大数据 来源:网络整理
导读:请考虑以下代码 Timer1 .Enabled := False;Timer1.Interval : = 300;For I := 1 to NumberOfTimesNeed doBegin Timer1 .Enabled := False; // Timer1 .Enabled := True; // reset the timer to 0.30 seconds TakesToLong := False; DoSomethingThatTakesTime
请考虑以下代码
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实现了不同的实现(不记得如何,我很久以前就清除了该代码). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |