简体   繁体   中英

How to handle OnTimer event in Delphi XE2?

I need to show a message say "Hi" everyday at 9 AM. Do I require Timer for this? How can I check whether its 9 AM or not. What should be the interval of timer at which OnTimer event run?

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.

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

在此输入图像描述

你可以使用类似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. The needed steps are:

  1. Put the timer on your form;
  2. Set the "interval" property to 1000 (one second) or less. For greater precision, you can set the interval property to 1, and the program will do the checking each milisecond
  3. Write the handler for 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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