0

how to execute multiple posts requests at the same time, this code sample is only represent a single anonymous thread, total are 15, however, even with one thread the post response becomes slower by time, at first it starts fast, then it slows down until it nearly stops!, i couldn't figure out whats wrong, this my 9th attempt to solve this. I tried net client, TTask, but they has bugs, any ideas? i mean codes, Thanks.

procedure TForm1.Test99;
var
 lHTTP: TIdHTTP;
 Params,Reply: TStringList;
begin
  lHTTP := TIdHTTP.Create(nil);
  Params := TStringList.Create;
  Reply  := TStringList.Create;

 TThread.CreateAnonymousThread(procedure ()
 var
  i : integer;
 begin
   for i := 0 to Z.Count-1 do
 begin
   Params.Add('Id=' +Z.Strings[i]);
   try
     Reply.Text := lHTTP.Post('https://www.mxhs95.com/test', Params);
     if AnsiContainsStr(Reply.Text, 'id')
     then
     begin
      TThread.Synchronize (TThread.CurrentThread,
      procedure ()
              begin
                Memo1.Lines.Add( Z.Strings[i]);
                Label2.Caption := Memo1.Lines.Count.ToString;
              end);             
     end;  
 end;
 Finally

  end;

end;
end).Start;
end;
D.Luffy
  • 17
  • 1
  • This isn't the cause of your issue but the `Params` list grows by one value for each iteration: is it the desiderd behaviour? Moreover there's an extra `end` in the method. Anyway it seems to me a weak appoach to threading but it shouldn't be the cause of the problem you describe. – fantaghirocco Aug 01 '16 at 12:56
  • it dose not grow, it changes each time @fantaghirocco – D.Luffy Aug 01 '16 at 15:06
  • in order to avoid the list to grow, one should either populate the list like `params.Values['Id'] := Z.Strings[i];` or use `Params.Clear` before `Params.Add` – fantaghirocco Aug 01 '16 at 15:21
  • you should write that as an answer, thanks @fantaghirocco – D.Luffy Aug 01 '16 at 15:50
  • I thought it was a minor issue, not the root cause of your problem :D – fantaghirocco Aug 01 '16 at 15:53

0 Answers0