2021-11-27 Delphi中三種延時方法

http://www.delphitop.com/html/hanshu/102.html

Delphi中三種延時方法

在Delphi中,通??梢杂靡韵氯N方法來實現(xiàn)程序的延時,即TTtimer控件,Sleep函數(shù),GetTickCount函數(shù)。但是其精度是各不相同的。

一、三種方法的簡單介紹

1)TTtimer控件

TTtimer控件的實質(zhì)是調(diào)用Windows API定時函數(shù)SetTimer和KillTimer來實現(xiàn)的,并簡化了對WM_TIMER 消息的處理過程。通過設(shè)置OnTimer事件和Interval屬性,我們可以很方便的產(chǎn)生一些簡單的定時事件。

2)Sleep函數(shù)

Sleep函數(shù)用來使程序的執(zhí)行延時給定的時間值。Sleep的調(diào)用形式為Sleep(milliseconds),暫停當(dāng)前的進(jìn)程milliseconds毫秒。Sleep的實現(xiàn)方法其實也是調(diào)用Windows API的Sleep函數(shù)。例如:

sleep(1000); //延遲1000毫秒

Sleep會引起程序停滯,如果你延遲的時間較長的話,你的程序?qū)⒉荒軌蝽憫?yīng)延時期間的發(fā)生的其他消息,所以程序看起來好像暫時死機(jī)。

3)GetTickCount函數(shù)

在主程序中延時,為了達(dá)到延時和響應(yīng)消息這兩個目的,GetTickCount()構(gòu)成的循環(huán)就是一種廣為流傳的方法。例如:

procedure Delay(MSecs: Longint);
//延時函數(shù),MSecs單位為毫秒(千分之1秒)
var
FirstTickCount, Now: Longint;
begin
FirstTickCount := GetTickCount();
repeat
Application.ProcessMessages;
Now := GetTickCount();
until (Now - FirstTickCount >= MSecs) or (Now < FirstTickCount);
end;

delay(2000);

procedure TForm1.Button16Click(Sender: TObject);
var i,j:integer;
    tokenstr,b,c,d,e,f,m,n,x:string;
    a,p:TstringStream;
begin

           tokenstr:=edit2.Text;
          edit3.Text:='';
           edit4.Text:='';
           edit5.Text:='';
           edit6.Text:='';
           edit7.Text:='';
           label3.caption:='';
           label4.caption:='';
           label5.caption:='';
           label6.caption:='';
           label7.caption:='';
           label12.caption:='';
           label13.caption:='';
           label14.caption:='';


          b:='http://api.yyyzmpt.com/index.php/clients/online/setWork?token='+tokenstr+'&pid=793&t=3&number='+memo2.lines[0];

            a:= TstringStream.Create('',65001);
            IdHTTP1.HandleRedirects:=True;
            IdHTTP1.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Maxthon)';


            IdHTTP1.Get(b,a);
             d:=a.datastring;      //  網(wǎng)頁獲取的數(shù)據(jù)進(jìn)行格式化
              a.Free;

           c:= 'http://api.yyyzmpt.com/index.php/clients/sms/getSms?token='+tokenstr+'&project=793&number='+memo2.lines[0]+'&type=1' ;
         //    showmessage(d );


         for  i:=0 to 120 do
       begin
               p:= TstringStream.Create('',65001);
             IdHTTP1.HandleRedirects:=True;
            IdHTTP1.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Maxthon)';

               IdHTTP1.Get(c,p);
               m:=p.datastring;

                memo3.Lines.Add(m) ;
               p.Free;
               delay(2000);
       end;




      //     showmessage( midstr(d,10,1) );


    //    showmessage(   copy(d,pos('現(xiàn)在',d)+2,4));




end;







?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容