放一个timer组件, procedure TForm1.Timer1Timer(Sender: TObject); begin if (now()=strtotime('12:00')) or (now()=strtotime('00:00')) then showmessage('快给我加分了');end;
今天试了一下delphi的定时器,按每秒1次事件来算,采用每次累加的方法,每累加到10分钟就去执行一个事件,同时记录下当前时间,结果发现,执行几遍之后,就出现了定时器消息丢失的情况。 代码如下: procedure TForm1.Timer1Timer(Sender: TObject); var time1:TDateTime; str:String; begin time1 := Time; str := TimeToStr(time1); Label7.Caption := str;
if bSyncStartFlag then begin internal := internal + 1; if internal = 60*10 then begin time1 := Time; str := TimeToStr(time1); Label7.Caption := str; str := 'start sync at '+ str; OutputLog(str); dosometing; internal := 0; end; end else begin if str = '18:00:00' then begin bSyncStartFlag := true; str := 'start sync at '+ str; OutputLog(str); dosometing; end; end; 以第一次事件从18:00:00开始计算,第二次就不准了,第二次的时间为18:10:03,说明漏了三个定时器消息,巨晕倒。
showmessage('dfasdf');
或者timer每秒触发一次,取得当前时间,如果条件相符就执行设定程序
方法是多种多样的。
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if (now()=strtotime('12:00')) or (now()=strtotime('00:00')) then
showmessage('快给我加分了');end;
我在C语言下的做法时:
1、取得当前时间;
2、计算得出到定时器设定时间的差并设为定时器的internal;
3、启动定时器;
4、在OnTimer事件里重启一次定时器。按照这个思路的话,如果一天一次闹钟,我一天处理一次就足够了。按照每秒算一次的方案,您一天得处理多少次?60*60*24!!!!
而且每秒起一次定时器的方法我也会的,只不过觉得太土,所以不好意思往上贴,所以才请教各位高人的。
var
nowtime:string;
begin
nowtime:= FormatDateTime('hh:nn:ss',now());
end;2、计算得出到定时器设定时间的差并设为定时器的internal;
(假设为12:00:00)var
thetime:string;
hh:integer;
nn:integer;
ss:integer;
thesec:integer; //总秒数
hh:=strtoint(copy(nowtime,1,2))-strtoint(copy(thetime,1,2));
nn:=strtoint(copy(nowtime,4,2))-strtoint(copy(thetime,4,2));
ss:=strtoint(copy(nowtime,7,2))-strtoint(copy(thetime,7,2)); thesec:=hh*3600+nn*60+ss;3.启动定时器;
timer1.enable:=true;4、在OnTimer事件里重启一次定时器。timer1.enable:=false;
timer1.enable:=true;
详细的自已要补充喔。
代码如下:
procedure TForm1.Timer1Timer(Sender: TObject);
var
time1:TDateTime;
str:String;
begin
time1 := Time;
str := TimeToStr(time1);
Label7.Caption := str;
if bSyncStartFlag then
begin
internal := internal + 1;
if internal = 60*10 then
begin
time1 := Time;
str := TimeToStr(time1);
Label7.Caption := str;
str := 'start sync at '+ str;
OutputLog(str);
dosometing;
internal := 0;
end; end
else
begin if str = '18:00:00' then
begin
bSyncStartFlag := true;
str := 'start sync at '+ str;
OutputLog(str);
dosometing;
end;
end;
以第一次事件从18:00:00开始计算,第二次就不准了,第二次的时间为18:10:03,说明漏了三个定时器消息,巨晕倒。
semigroup的主意倒是不错
你用timer客户就会买菜刀去的
e-mail:[email protected]