Giving the following code:
procedure TForm1.Button1Click(Sender: TObject);
var
myTask: ITask;
begin
myTask := TTask.Run(
procedure
var
i: Integer;
begin
for i := 0 to 3 do
begin
TThread.ForceQueue(
nil
, procedure
begin
ShowMessage('Count: ' + i.ToString);
end
);
//I know this will work
// TThread.Synchronize(
// nil
// , procedure
// begin
// ShowMessage('Count: ' + i.ToString);
// end
// );
end;
end
);
end;
This will always print "Count: 4"
Is there a way to keep the variable i
when the queued anonymous procedure is executed?
I know it will work using Synchronize()
, but I prefer using Queue
because in my real case the queued method writes some log in a file. My task doen't need to wait for the log, that's why I prefer using Queue()
.
I'm using Delphi 10.3.