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;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM