Das Daten holen und Verarbeiten kannst du natürlich auch Trennen.
Das Verarbeiten wiederum kann nochmals parallelisiert werden, falls notwendig.
Delphi-Quellcode:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.Threading,
System.Generics.Collections;
type
IResponse =
interface
['
{E705E40C-9BC2-41EE-AE4B-2E1D514395E9}']
function HasMoreData: Boolean;
end;
TResponse =
class(TInterfacedObject, IResponse)
private
class var
FCounter: Integer;
public
function HasMoreData: Boolean;
end;
TMyClass =
class
private
FRespones: TList<IResponse>;
public
constructor Create();
destructor Destroy;
override;
procedure GetData;
function GetDataFromServer: IResponse;
procedure DoSomeGreatWork(
const AResponse: IResponse);
end;
constructor TMyClass.Create;
begin
inherited;
FRespones := TList<IResponse>.Create;
end;
destructor TMyClass.Destroy;
begin
FRespones.Free;
inherited;
end;
procedure TMyClass.DoSomeGreatWork;
begin
Writeln('
Make Work Great Again!');
end;
procedure TMyClass.GetData;
var
MoreData: Boolean;
Response: IResponse;
begin
repeat
Response := GetDataFromServer;
FRespones.Add(Response);
MoreData := Response.HasMoreData;
Writeln('
HasMoreData: ', MoreData);
until not MoreData;
for Response
in FRespones
do
begin
DoSomeGreatWork(Response);
end;
Writeln('
Fix und fertig!');
end;
function TMyClass.GetDataFromServer: IResponse;
begin
Result := TResponse.Create;
Sleep(50);
end;
function TResponse.HasMoreData: Boolean;
begin
Result := True;
Inc(FCounter);
if FCounter = 100
then
Result := False;
end;
procedure Main;
begin
TTask.Run(
procedure
var
MyObject: TMyClass;
begin
MyObject := TMyClass.Create;
try
MyObject.GetData;
finally
MyObject.Free;
end
end);
end;
begin
try
Main;
Readln;
except
on E:
Exception do
Writeln(E.ClassName, '
: ', E.
Message);
end;
end.