今天做了个IDHTTP.get采集网页的时候,网站网络情况不好,界面就卡死了,通过百度发现使用下面的方法可以实现水卡死情况
放一个TIdAntiFreezeControl控件,然后在执行的任务前面加上下面这句即可
IdAntiFreeze1.OnlyWhenIdle:=False;//设置使有反应
比如:
procedure TForm1.Button2Click(Sender: TObject);
var
MyStr:String;
Dcphttp:TIdHTTP;
IsUtf8 :Boolean;
begin
IdAntiFreeze1.OnlyWhenIdle:=False;//设置使有反应.
Dcphttp:=TIdHTTP.Create(Application);
IsUtf8:=False;
try
Dcphttp.ReadTimeout:=5000;
Dcphttp.ConnectTimeout:=5000;
Dcphttp.HandleRedirects := True;
Dcphttp.Request.Accept:='text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8';
Dcphttp.Request.ProxyConnection:='keep-alive';
Dcphttp.Request.UserAgent := 'Mozilla/5.0';
MyStr := Dcphttp.Get(Urle+'?SortType=desc&DType=Lastest1Day');
if IsUtf8 then
MyStr:= Utf8ToAnsi(MyStr);
Memo1.Text:=MyStr;
except
Showmessage('网络出错!');
Exit;
end;
FreeAndNil(dcphttp);
end;