gpt4 book ai didi

delphi - 德尔福定时器

转载 作者:行者123 更新时间:2023-12-03 15:15:24 24 4
gpt4 key购买 nike

考虑以下代码

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 中实现方式不同(不记得是如何实现的,我很久以前就清除了该代码)。

关于delphi - 德尔福定时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/525416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com