Вот пример моего потока загрузки
Запуск:
unit DownloadT;
interface
uses
System.Classes, System.Net.URLClient, System.Net.HttpClient, System.Net.HttpClientComponent,
System.SysUtils;
type
TonDownloadProgress = procedure (Progress:integer) of object;
TonDownloadEnd = procedure (FileName:string; Done:boolean; MessageText:string='') of object;
TDownloadThread = class(TThread)
private
IdHTTP1: TNetHTTPClient;
fonDownloadProgress:TonDownloadProgress;
FonDownloadEnd: TonDownloadEnd;
Done: boolean;
Max: integer;
ProgressValue: integer;
procedure ReceiveData(const Sender: TObject; AContentLength:int64; AReadCount:int64; var Abort:boolean);
procedure SetonDownloadProgress(const Value: TonDownloadProgress);
procedure SetonDownloadEnd(const Value: TonDownloadEnd);
protected
procedure Execute; override;
public
url, Status: string;
Filename: string;
property onDownloadProgress: TonDownloadProgress read FonDownloadProgress write SetonDownloadProgress;
property onDownloadEnd: TonDownloadEnd read FonDownloadEnd write SetonDownloadEnd;
end;
implementation
{ TDownloadThread }
procedure TDownloadThread.Execute;
var
MyFile: TMemoryStream;
begin
FreeOnTerminate := true;
IdHTTP1 := TNetHTTPClient.Create(nil);
MyFile := TMemoryStream.Create();
try
IdHTTP1.OnReceiveData := ReceiveData;
IdHTTP1.HandleRedirects := true;
MyFile.Position := 0;
IdHTTP1.Get(url, MyFile);
MyFile.SaveToFile(Filename);
Done := true;
MyFile.Free;
IdHTTP1.Free;
if Assigned(FonDownloadEnd) then
Synchronize(procedure begin
FonDownloadEnd(Filename, True);
end);
except
on E:Exception do
if Assigned(FonDownloadEnd) then
Synchronize(procedure begin
FonDownloadEnd(Filename, False, E.Message);
end);
end;
end;
procedure TDownloadThread.ReceiveData(const Sender: TObject ; AContentLength:int64 ; AReadCount:int64 ;var Abort:boolean);
begin
if Terminated then
Abort := true
else
begin
Done := false;
ProgressValue := AReadCount;
if Assigned(fonDownloadProgress) then
Synchronize(procedure begin
if AContentLength>0 then
fonDownloadProgress(Trunc(ProgressValue / AContentLength * 100))
else
fonDownloadProgress(0); end);
end;
end;
procedure TDownloadThread.SetonDownloadEnd(const Value: TonDownloadEnd);
begin
FonDownloadEnd := Value;
end;
procedure TDownloadThread.SetonDownloadProgress(const Value: TonDownloadProgress);
begin
FonDownloadProgress := Value;
end;
end.
Запуск:
procedure TMainForm.InstallUpdates(aDownloadLink:string);
begin
DownloadThread:=TDownloadThread.Create(true);
DownloadThread.Filename := TPath.Combine(Options.WorkPath,'updates.exe');
DownloadThread.url := aDownloadLink;
DownloadThread.onDownloadEnd := OnDownloadEnd;
DownloadThread.onDownloadProgress := OnDownloadProgress;
DownloadThread.Start;