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<MaxWaitTime)   do
          begin
                  SleepEx(100,False);
                  Inc(WaitedTime,100);
                  Application.ProcessMessages ;
          end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章