Thread termination with http get request termination

I have this code inside thread and when I call terminate it terminates fast if it is not in the middle of http request , but when I have 20 threads it takes time to finish http request and exit the thread. CODE:

    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;

How do I stop this HTTP request from main form :

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

Thanks

Edit: Termination code :

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

    end;

Upvotes: 0

Views: 444

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595792

Call the THttpSend.Abort() method to stop a transfer that is in progress. You can do that in the THttpSend.OnStatus event, eg:

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;

Upvotes: 1

Related Questions