简体   繁体   English

如何在Delphi XE2中处理OnTimer事件?

[英]How to handle OnTimer event in Delphi XE2?

I need to show a message say "Hi" everyday at 9 AM. 我需要每天上午9点显示一条消息“嗨”。 Do I require Timer for this? 我需要Timer吗? How can I check whether its 9 AM or not. 我怎样才能检查是否是上午9点。 What should be the interval of timer at which OnTimer event run? OnTimer事件运行的计时器间隔应该是多少?

procedure Form1.TimerTimer1(Sender: TObject);
begin
  ShowMessage("Hi");
end;

If I run this event in after 24 hours, I fear it might pass 9 AM and will not fire. 如果我在24小时后举办此活动,我担心它可能会在上午9点过去而不会开火。

Unless you have other valid reasons, it's far more easier to 除非你有其他正当理由,否则要容易得多

  • write a simple application that shows the message and quits 编写一个显示消息并退出的简单应用程序
  • schedule it to run using the task scheduler of Windows 安排它使用Windows的任务调度程序运行

在此输入图像描述

你可以使用类似CRON的Delphi解决方案: http//www.cromis.net/blog/downloads/cron-scheduler/

As the answerers before me said, there are better and easier ways. 正如我之前的回答者所说,有更好更容易的方法。 Suppose you want to do this your way, in Delphi, then yes, you need a timer. 假设您想按照Delphi的方式执行此操作,那么是的,您需要一个计时器。 The needed steps are: 所需的步骤是:

  1. Put the timer on your form; 把计时器放在你的表格上;
  2. Set the "interval" property to 1000 (one second) or less. 将“interval”属性设置为1000(一秒)或更短。 For greater precision, you can set the interval property to 1, and the program will do the checking each milisecond 为了获得更高的精度,可以将interval属性设置为1,程序将检查每个毫秒
  3. Write the handler for OnTimer: 编写OnTimer的处理程序:

     procedure Form1.TimerTimer1(Sender: TObject); var x:TDateTime begin x:=Now; if {the hour read is 9 and minute is 0} then ShowMessage("Hi"); end; 

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM