Delphi中进行延时的4种方法

 1、挂起,不占CPU
sleep
2、不挂起,占cpu
procedure Delay(msecs:integer);
var
FirstTickCount:longint;
begin
FirstTickCount:=GetTickCount;
repeat
Application.ProcessMessages;
until ((GetTickCount-FirstTickCount) >= Longint(msecs));
end;
3、定时器
procedure timerfun(handle:Thandle;msg:word;identer:word;dwtime:longword);stdcall;
begin
showmessage('到点了');
killtimer(handle,identer);//关闭定时器
end;

//其中的identer是定时器的句柄
procedure TForm1.Button1Click(Sender: TObject);
var
identer:integer;
begin
   identer:=settimer(0,0,2000,@timerfun);
   if identer=0 then exit; //定时器没有创建成功。
end;
4、不占CPU不挂起
function TForm1.HaveSigned(MaxWaitTime: Cardinal): Boolean;
var   I:Integer;
var   WaitedTime:Cardinal;
begin
          WaitedTime:=0;
          while      (WaitedTime
          begin
                  SleepEx(100,False);
                  Inc(WaitedTime,100);
                  Application.ProcessMessages ;
          end
end;

posted on   癫狂编程  阅读(3309)  评论(0编辑  收藏  举报

努力加载评论中...

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
好的代码像粥一样,都是用时间熬出来的
点击右上角即可分享
微信分享提示