unit WebUpdateThread;
interface
uses
classes, IdComponent, IdHTTP;
type
TOnWorkEvent =
procedure(Sender: TThread; AWorkCount: Integer)
of object;
TDownloadThread =
Class (TThread)
private
fIdHTTP: TIdHTTP;
fName:
string;
// Name einer herunterzuladenden Datei
fPfad:
string;
// wohin soll die Datei auf Platte gespeichert werden
fURL:
string;
// URL der Datei
fWorkCount: integer;
fOnWorkEvent: TOnWorkEvent;
procedure InternalOnWork (Sender: TObject; AWorkMode: TWorkMode;
const AWorkCount: Integer);
procedure DoNotifyWork;
protected
procedure execute;
override;
public
property name:
string read fName
write fName;
property Pfad:
string read fPfad
write fPfad;
property URL:
string read fURL
write fURL;
property OnWork: TOnWorkEvent
read FOnWorkEvent
write FOnWorkEvent;
end;
implementation
uses
SysUtils;
procedure TDownloadThread.Execute;
var
fs: TFileStream;
begin
fIdHTTP := TIdHTTP.Create(
nil);
fIdHTTP.OnWork := InternalOnWork;
fs := TFileStream.Create (fPfad + fName, fmCreate
or fmShareExclusive);
try
fIdHTTP.Get(fURL + fName, fs);
finally
fs.Free;
fIdHTTP.Free;
end;
end;
procedure TDownloadThread.InternalOnWork(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCount: Integer);
begin
FWorkCount := AWorkCount;
Synchronize(DoNotifyWork);
end;
procedure TDownloadThread.DoNotifyWork;
begin
if Assigned(OnWork)
then
OnWork (Self, FWorkCount);
end;
end.