首页  编辑  

延时指定时间

Tags: /超级猛料/Date.Time.时间和日期/   Date Created:

// 1. Delay

procedure Delay(dwMilliseconds: Longint);

var

 iStart, iStop: DWORD;

begin

 iStart := GetTickCount;

 repeat

   iStop := GetTickCount;

   Application.ProcessMessages;

 until (iStop - iStart) >= dwMilliseconds;

end;

// 2. Delay: with API

procedure Delay(msecs: Longint);

var

 targettime: Longint;

 Msg: TMsg;

begin

 targettime := GetTickCount + msecs;

 while targettime < GetTickCount do

   if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then

   begin

     TranslateMessage(Msg);

     DispatchMessage(Msg);

   end;

end;

{

 Note:

 The elapsed time is stored as a DWORD value.

 Therefore, the time will wrap around to zero if the system is

 run continuously for 49.7 days.

}

// 3. Sleep

{

 The Sleep function suspends the execution of the current

 thread for a specified interval.

}

Sleep(dwMilliseconds: Word);

// 4. Combined Delay

{

 Including the Sleep in the loop prevents the app from hogging

 100% of the CPU for doing practically nothing but running around the loop.

}

procedure PauseFunc(delay: DWORD);

var

 lTicks: DWORD;

begin

 lTicks := GetTickCount + delay;

 repeat

   Sleep(100);

   Application.ProcessMessages;

 until (lTicks <= GetTickCount) or Application.Terminated;

end;

还可以用SleepEx函数。