首页 > 代码库 > [转]Delphi中进行延时的4种方法

[转]Delphi中进行延时的4种方法

1、挂起,不占CPUsleep2、不挂起,占cpuprocedure Delay(msecs:integer);varFirstTickCount:longint;beginFirstTickCount:=GetTickCount;repeatApplication.ProcessMessages;until ((GetTickCount-FirstTickCount) >= Longint(msecs));end;3、定时器procedure timerfun(handle:Thandle;msg:word;identer:word;dwtime:longword);stdcall;beginshowmessage(‘到点了‘);killtimer(handle,identer);//关闭定时器end;//其中的identer是定时器的句柄procedure TForm1.Button1Click(Sender: TObject);varidenter: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 ;          endend;

[转]Delphi中进行延时的4种方法