[SOLVED] TidHttp and thread lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- superc · 2022-02-10 13:16 Hello, I'm must to download a file and i used TidHttp with this simple code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  tm : TMemoryStream;  pk: string;begin  try    tm := TMemoryStream.Create;     http.Get(FDOWN, tm);    tm.SaveToFile('c:\td\test.fl');   finally  tm.Free;  end;end; and works fine, but i noticed that that the application remains freezed: is the nature of this component to be used inside a thread? Thanks in advance. ---------------------------------------------------------------------- Remy Lebeau · 2022-02-10 17:50 Quote from: superc on February 10, 2022, 02:16:29 pmi noticed that that the application remains freezed: is the nature of this component to be used inside a thread? All of Indy's components are blocking.  So, if you use them in the context of the main UI thread, then the UI will indeed freeze up until the operations are finished.  To avoid that, you can use a separate worker thread, yes (and that is preferred, even required in some cases [ahem, Android]).  Or, you can simply put down a TIdAntiFreeze component instead (may not work on all platforms). ---------------------------------------------------------------------- superc · 2022-02-11 08:24 Quote from: Remy Lebeau on February 10, 2022, 06:50:51 pmQuote from: superc on February 10, 2022, 02:16:29 pmi noticed that that the application remains freezed: is the nature of this component to be used inside a thread? All of Indy's components are blocking.  So, if you use them in the context of the main UI thread, then the UI will indeed freeze up until the operations are finished.  To avoid that, you can use a separate worker thread, yes (and that is preferred, even required in some cases [ahem, Android]).  Or, you can simply put down a TIdAntiFreeze component instead (may not work on all platforms). Thanks Remy, I've tried a code like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit httpThreadDownloader; {$mode ObjFPC}{$H+} interface uses  Classes, IdHTTP, IdSSLOpenSSL,IdComponent, SysUtils; type   TCallBack = procedure(Sender: TObject; oPercentage: Integer) of object;   { TDownloaderThread }   TDownloaderThread = class (TThread)    callBck: TCallBack;  private    ctrlVar         : Boolean;    fDownloFname    : string;    fhttpEPD        : string;    _http           : TIdHTTP;    _socketOpenssl  : TIdSSLIOHandlerSocketOpenSSL;    _byteToTransfer : Int64;  public    Constructor Create(CreateSuspended : boolean); overload;    constructor Create(CreateSuspended : Boolean; aCallBack: TCallBack);     procedure IdHTTP1Work(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);    procedure IdHTTP1WorkBegin(ASender: TObject; AWorkMode: TWorkMode;AWorkCountMax: Int64);    procedure IdHTTP1Status(ASender: TObject; const AStatus: TIdStatus; const AStatusText: string);    procedure IdHTTP1WorkEnd(ASender: TObject; AWorkMode: TWorkMode);     property httpEndPointDownload: string read fhttpEPD write fhttpEPD;    property downloadFilename: string read fDownloFname write fDownloFname;  protected    procedure Execute; override;  end; implementation { TDownloaderThread }  constructor TDownloaderThread.Create(CreateSuspended: boolean);begin  inherited Create(CreateSuspended);   FreeOnTerminate := True;   _http := TIdHTTP.Create;   _http.OnWork      := @IdHTTP1Work;  _http.OnWorkBegin := @IdHTTP1WorkBegin;  _http.OnStatus    := @IdHTTP1Status;  _http.OnWorkEnd   := @IdHTTP1WorkEnd;    _socketOpenssl := TIdSSLIOHandlerSocketOpenSSL.Create;  _socketOpenssl.SSLOptions.Method := sslvTLSv1_2;  _http.IOHandler   := _socketOpenssl; end; constructor TDownloaderThread.Create(CreateSuspended: Boolean;                                   aCallBack: TCallBack);begin  inherited Create(CreateSuspended);   FreeOnTerminate := True;   _http := TIdHTTP.Create;  _socketOpenssl := TIdSSLIOHandlerSocketOpenSSL.Create;  _socketOpenssl.SSLOptions.Method := sslvTLSv1_2;  _http.IOHandler := _socketOpenssl;   if aCallBack <> nil then    callBck:=aCallBack;end; procedure TDownloaderThread.IdHTTP1Work(ASender: TObject; AWorkMode: TWorkMode;  AWorkCount: Int64);var  x: Integer;begin  x := Round((AWorkCount / _byteToTransfer) * 100);  callBck(self, x);end; procedure TDownloaderThread.IdHTTP1WorkBegin(ASender: TObject;  AWorkMode: TWorkMode; AWorkCountMax: Int64);begin  _byteToTransfer:=AWorkCountMax;end; procedure TDownloaderThread.IdHTTP1Status(ASender: TObject;  const AStatus: TIdStatus; const AStatusText: string);begin end; procedure TDownloaderThread.IdHTTP1WorkEnd(ASender: TObject;  AWorkMode: TWorkMode);begin end; procedure TDownloaderThread.Execute;var  tm :TMemoryStream;begin  try    tm := TMemoryStream.Create;    _http.Get(httpEndPointDownload, tm);    tm.SaveToFile(downloadFilename);   finally    tm.Free;  end;end; end.   But the IdHTTP1Work, IdHTTP1WorkBegin,IdHTTP1Status,IdHTTP1WorkEnd procedure seems not correctly hooked to component.... On the example there are two methods with thread and not... What's I wrong? Thanks in advance. ---------------------------------------------------------------------- superc · 2022-02-11 12:18 I found error: on overloaded constructor I don't set procedures.