http://www.delphitop.com/html/hanshu/102.html
Delphi中三种延时方法
在Delphi中,通常可以用以下三种方法来实现程序的延时,即TTtimer控件,Sleep函数,GetTickCount函数。但是其精度是各不相同的。
一、三种方法的简单介绍
1)TTtimer控件
TTtimer控件的实质是调用Windows API定时函数SetTimer和KillTimer来实现的,并简化了对WM_TIMER 消息的处理过程。通过设置OnTimer事件和Interval属性,我们可以很方便的产生一些简单的定时事件。
2)Sleep函数
Sleep函数用来使程序的执行延时给定的时间值。Sleep的调用形式为Sleep(milliseconds),暂停当前的进程milliseconds毫秒。Sleep的实现方法其实也是调用Windows API的Sleep函数。例如:
sleep(1000); //延迟1000毫秒
Sleep会引起程序停滞,如果你延迟的时间较长的话,你的程序将不能够响应延时期间的发生的其他消息,所以程序看起来好像暂时死机。
3)GetTickCount函数
在主程序中延时,为了达到延时和响应消息这两个目的,GetTickCount()构成的循环就是一种广为流传的方法。例如:
procedure Delay(MSecs: Longint);
//延时函数,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; // 网页获取的数据进行格式化
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('现在',d)+2,4));
end;
网友评论