使用http get请求终止的线程终止

时间:2012-04-02 12:35:44

标签: delphi-7

我在线程中有这个代码,当我调用terminate时,如果它不在http请求的中间,它会快速终止,但是当我有20个线程时,需要时间来完成http请求并退出线程。 代码:

    procedure TParser.Execute;
var

   i,b:integer;
   t:string;
   begin
http := thttpsend.create;
http.KeepAlive:=true;
Ftest:=TStringList.Create;
{http.timeout:=5000;}
for i:=0 to FStartNum.Count do

if  FLocalVariable<FStartNum.Count-FThreadCount   then
begin
if terminated then begin
exit;
end
else

EnterCriticalSection(Form1.StringSection);
 try



FLocalVariable := form1.GlobalVariable;
Inc(FLocalVariable);
form1.GlobalVariable := FLocalVariable;


 finally

LeaveCriticalSection(Form1.StringSection);
http.Clear;

HTTP.HTTPMethod('GET',FStartNum.Strings[FLocalVariable]);
      Ftest.LoadFromStream(HTTP.Document);

      Synchronize(progress);
parse;
Ftest.Clear;

end;
end;
end;

如何从主窗体中停止此HTTP请求:

HTTP.HTTPMethod('GET',FStartNum.Strings[FLocalVariable]);
          Ftest.LoadFromStream(HTTP.Document);

由于

编辑:终止代码:

for i:=Low(fparser) to High(fparser) do
    begin
    Fparser[i].Terminate();

    end;

1 个答案:

答案 0 :(得分:1)

调用THttpSend.Abort()方法停止正在进行的传输。您可以在THttpSend.OnStatus事件中执行此操作,例如:

procedure TParser.Execute;
var
  ...
begin
  http := THttpSend.Create;
  http.OnStatus := HttpStatus;
  ...
end;

procedure TParser.HttpStatus(Sender: TObject; Reason: THookSocketReason; const Value: String);
begin
  if Terminated then Http.Abort;
end;