Do not tell me why hangs? (the code is executed, but the form hangs) Or is it possible to send requests with random delay in another way?

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, IdCookieManager, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, ExtCtrls, Buttons; type TForm1 = class(TForm) IdHTTP1: TIdHTTP; IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL; IdCookieManager1: TIdCookieManager; Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); end; TNewThread = class(TThread) private Progress: string; procedure SetProgress; protected procedure Execute; override; end; var Form1: TForm1; implementation {$R *.dfm} procedure TNewThread.Execute; begin Synchronize(SetProgress); end; procedure TForm1.Button1Click(Sender: TObject); var NewThread: TNewThread; begin NewThread:=TNewThread.Create(False); NewThread.Priority:=tpNormal; NewThread.Resume; NewThread.FreeOnTerminate:=true; end; procedure TNewThread.SetProgress; var list, lista: TStringList; int, i: Integer; s, b: string; resul: string; begin ...другой ненужный код... resul := Form1.IdHTTP1.Get('https://...'); while Pos('images/icons/tb_stocked', resul) > 0 do begin lista := TStringList.Create; lista.Add('...'); Form1.IdHTTP1.Post('https://...',lista); Sleep(10000); lista.Free; end; end; end; end. 

    1 answer 1

    The form freezes because you do all the work inside the Synchronize call. Thereby, you give up everything that a thread can give you and in fact executes it in the main thread (in which the entire GUI works).

    Learn to work with streams - their essence and strength is to perform the bulk of work in the background, and report only the results to the main flow. Look for the tutorial.