单次计时器 [英] One-Shot Timers

查看:319
本文介绍了单次计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的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):


  1. 调用一个程序串口

  2. 启用一个X量为间隔的定时器

  1. Call a procedure which send a char over serial port
  2. 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?

谢谢! p>

Thank you!

推荐答案

是否可以从定时器事件中删除计时器?

Is it safe to kill timer from inside of a timer event ?

是的,这是非常安全的。

Yes, that's perfectly safe.

如何实现简单的一次性定时器?

How to implement simplest one shot timer ?

这是一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆