unit ThreadDownload;
interface
uses
System.Classes, ......
type
TDownloadThread =
class(TThread)
constructor Create;
destructor Destroy;
override;
private
FCS: TCriticalSection;
IdFTP1: TIdFTP;
progressvalue: int64;
progressmax: int64;
procedure IdFTPWorkBegin(ASender: TObject; AWorkMode: TWorkMode;
AWorkCountMax: int64);
procedure IdFTPWork(ASender: TObject; AWorkMode: TWorkMode;
AWorkCount: int64);
procedure IdFTPWorkEnd(ASender: TObject; AWorkMode: TWorkMode);
procedure Updatelabel;
protected
procedure Execute();
override;
public
FileName:
string;
property CS: TCriticalSection
read FCS;
end;
var
Thread_Download: TDownloadThread;
implementation
uses UnitUpdate;
constructor TDownloadThread.Create;
begin
FreeOnTerminate := True;
// Suspended starten
inherited Create(True);
try
progressvalue := 0;
progressmax := 1000;
FCS := TCriticalSection.Create;
except
on E:
Exception do
LogAusgabe(E.ClassName + '
in TDownloadThread.Create : ' +
E.
Message, True);
end;
end;
destructor TDownloadThread.Destroy;
begin
try
// was sonst noch zerstört werden muss
FCS.DisposeOf;
IdFTP1.DisposeOf;
// globale Variable rücksetzen
Thread_Download :=
nil;
except
on E:
Exception do
LogAusgabe(E.ClassName + '
in TDownloadThread.Destroy : ' +
E.
Message, True);
end;
inherited;
end;
procedure TDownloadThread.Execute;
var
anz: integer;
begin
anz := 0;
try
IdFTP1 := TIdFTP.Create(
nil);
IdFTP1.IPVersion := id_IPv4;
IdFTP1.TransferType := ftBinary;
IdFTP1.OnWork := IdFTPWork;
IdFTP1.OnWorkBegin := IdFTPWorkBegin;
IdFTP1.OnWorkEnd := IdFTPWorkEnd;
IdFTP1.Host := FTPHost;
IdFTP1.Username := FTPUser;
IdFTP1.Password := DecryptFTP(FTPKey);
IdFTP1.Passive := True;
IdFTP1.Connect();
// ist die gewünschte Datei überhaupt vorhanden?
// wenn nein gibt Size -1 zurück!
progressmax := IdFTP1.Size(FileName);
if progressmax = -1
then
begin
IdFTP1.Disconnect;
Terminate;
end;
try
IdFTP1.Get(FileName, DownloadDir + FileName, True);
except
IdFTP1.Disconnect;
end;
Terminate;
except
on E:
Exception do
LogAusgabe(E.ClassName + '
in TDownloadThread.Execute : ' +
E.
Message, True);
end;
end;
procedure TDownloadThread.IdFTPWorkBegin(ASender: TObject; AWorkMode: TWorkMode;
AWorkCountMax: int64);
begin
progressvalue := 0;
Synchronize(Updatelabel);
end;
procedure TDownloadThread.IdFTPWork(ASender: TObject; AWorkMode: TWorkMode;
AWorkCount: int64);
begin
progressvalue := AWorkCount;
Synchronize(Updatelabel);
end;
procedure TDownloadThread.IdFTPWorkEnd(ASender: TObject; AWorkMode: TWorkMode);
begin
progressvalue := 0;
Synchronize(Updatelabel);
end;
procedure TDownloadThread.Updatelabel;
begin
FormUpdate.Updatelabel(progressvalue)
end;
end.