unit Unit1;
interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes, System.Threading,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
type
pMyRecord = ^TMyRecord;
TMyRecord = packed record
Color: Integer;
AllowNewColor: Boolean;
Next: pMyRecord;
end;
var
Form1: TForm1;
RecordArraySart: pMyRecord;
RecordArraySize: Integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
fCurrentRecordArray: pMyRecord;
begin
// create dummy structure
RecordArraySize:= 0;
New(RecordArraySart);
fCurrentRecordArray:= RecordArraySart;
fCurrentRecordArray^.Color:= -1;
fCurrentRecordArray^.AllowNewColor:= True;
fCurrentRecordArray^.Next:= nil;
Inc(RecordArraySize);
for i:= 0 to 18 do begin
New(fCurrentRecordArray^.Next);
fCurrentRecordArray:= fCurrentRecordArray^.Next;
fCurrentRecordArray^.Color:= -1;
fCurrentRecordArray^.AllowNewColor:= True;
fCurrentRecordArray^.Next:= nil;
Inc(RecordArraySize);
end;
end;
procedure TaskWorker(ipMyRecord: pMyRecord);
begin
Randomize;
if ipMyRecord^.AllowNewColor then
ipMyRecord^.Color:= Random(30000);
end;
procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
fCurrentRecordArray: pMyRecord;
fTaskNo: Integer;
fTasks: array of ITask;
begin
SetLength(fTasks, RecordArraySize);
fTaskNo:= 0;
fCurrentRecordArray:= RecordArraySart;
for i:= 0 to RecordArraySize - 1 do begin
fTasks[fTaskNo]:= TTask.Create(procedure()
// new Task
begin
TaskWorker(fCurrentRecordArray);
end);
fTasks[fTaskNo].Start;
fCurrentRecordArray:= fCurrentRecordArray^.Next;
Inc(fTaskNo);
end;
TTask.WaitForAll(fTasks);
Memo1.Lines.Clear;
for i:= 0 to RecordArraySize - 1 do begin
fCurrentRecordArray:= RecordArraySart;
Memo1.Lines.Add(IntToStr(fCurrentRecordArray^.Color));
fCurrentRecordArray:= fCurrentRecordArray^.Next;
end;
end;
end.