熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

Delphi中進行延時的4種方法

2022-06-13   來源: .NET編程 

  掛起不占CPU

    sleep

  不掛起占cpu

     procedure Delay(msecs:integer);
        var
        FirstTickCount:longint;
        begin
        FirstTickCount:=GetTickCount;
        repeat
        ApplicationProcessMessages;
        until ((GetTickCountFirstTickCount) >= Longint(msecs));
        end;

  定時器

      procedure timerfun(handle:Thandle;msg:word;identer:word;dwtime:longword);stdcall;
        begin
        showmessage(到點了);
        killtimer(handleidenter);//關閉定時器
        end;
        //其中的identer是定時器的句柄
        procedure TFormButtonClick(Sender: TObject);
        var
        identer:integer;
        begin
           identer:=settimer(@timerfun);
           if identer= then exit; //定時器沒有創建成功
        end;

  不占CPU不掛起

    function TFormHaveSigned(MaxWaitTime: Cardinal): Boolean;
        var   I:Integer;
        var   WaitedTime:Cardinal;
        begin
                  WaitedTime:=;
                  while      (WaitedTime<MaxWaitTime)   do
                  begin
                          SleepEx(False);
                          Inc(WaitedTime);
                          ApplicationProcessMessages ;
                  end
        end;


From:http://tw.wingwit.com/Article/program/net/201311/13342.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.