一键式计时器 [英] One-Shot Timers
问题描述
亲爱的 Delphi 程序员,
Dear Delphi programmers,
我正在寻求如何编写一次性计时器的帮助(没有 GUI,所以 VCL 计时器没有问题)...
I'm looking for help how to write a one-shot timer (No GUI, so VCL Timers out of question)...
让我再解释一下.
在我的代码中(用 VCL 计时器解释,但在这个特定项目中我没有表格):
In my code (explaining with VCL timer but in this particular project I have no forms):
- 调用一个
procedure
,通过串口发送一个字符 - 启用具有 X 数量的
Interval
的计时器
- Call a
procedure
which send a char over serial port - Enable a timer with a X amount of
Interval
在OnTimer
事件中:
我有一个代码,它发送一个字符然后禁用计时器本身,使其不再被执行.
I have a code which send a char then disable the timer itself to never be executed again.
问题是我需要动态创建这些计时器.我想到了OnTimer 事件"中的函数 SetTimer()
然后 KillTimer()
来禁用它(释放它).
The problem is that I need to make the creation of these timers dynamic.
I thought of the function SetTimer()
then KillTimer()
in the "OnTimer event" to disable it (free it).
这是一种好(安全)的方式吗?
Is it a good (safe) way?
谢谢!
推荐答案
从定时器事件内部杀死定时器是否安全?
是的,这非常安全.
如何实现最简单的一次性计时器?
1 秒一次计时器的最简单实现是这样,但请注意,如果您启动更多计时器,您将无法区分其中哪个计时器经过了其间隔:
The easiest implementation of a 1 second one shot timer is this, but note, that if you start more of them, you won't be able to distinguish which one of them elapsed its interval:
procedure TimerProc(hwnd: HWND; uMsg: UINT; idEvent: UINT_PTR;
dwTime: DWORD); stdcall;
begin
KillTimer(0, idEvent);
ShowMessage('I''m done!');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SetTimer(0, 0, 1000, @TimerProc);
end;
这篇关于一键式计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!