Zitat von
SirThornberry:
oder eben mit der Callbackvariante
Delphi-Quellcode:
TProgressEvnt = procedure(Sender: TObject; Progress: Integer) of Object;
TComplexDataObject = class(TObject)
private
fOnProgress: TProgressEvnt;
published
property OnProgress: TProgressEvnt read fOnProgress write fOnProgress;
end;
[...]
procedure TComplexDateObject.DeineBerechnung;
begin
//Do something
if Assigned(fOnProgress) then
fOnProgress(Self, x);
//Do something
end;
Ich halte das mit dem Callback auch für am sinnvollsten und flexibelsten. Das Beispiel stammt aus einer Klasse, die Datenstrukturen aus SAP einliest:
Delphi-Quellcode:
// Notify-Event for reading/writing of IDocs
TIDocProgressEvent = procedure(iStepBy, iMax: integer) of object;
...
property OnReadFromFile : TIDocProgressEvent read FOnReadFromFile write SetOnReadFromFile;
...
// Call notification for startup
if assigned(FOnReadFromFile) then
FOnReadFromFile(-1, iFileSize);
...
// Call progress function
if assigned(FOnReadFromFile) then
FOnReadFromFile(FilePosition-OldFilePosition,-1);
...
// Handler which is called while processing the input file
IDoc.OnReadFromFile := IDocFileProgress;
//------------------------------------------------------------------------------
procedure TfrmMain.IDocFileProgress(iStepBy, iMax: integer);
//------------------------------------------------------------------------------
// Notification which is called while processing the IDOC
// iStepBy : < 0 = Start
// > 0 = Number of lines processed in this step
//------------------------------------------------------------------------------
begin
if iStepBy < 0 then begin
ProgressBar1.Properties.Max := iMax;
ProgressBar1.Properties.Min := 0;
ProgressBar1.Position := 0;
end
else
Progressbar1.Position := ProgressBar1.Position+iStepBy;
end;