我需要将当前任务索引检索到ITask(TTask)的数组中,例如:
var
aTasks: array of ITask;
aResult : integer;
I: Integer;
begin
Setlength(aTasks, 3);
aResult := 0;
for I := Low(aTasks) to High(aTasks) do begin
aTasks[I] := TTask.Create(procedure()
begin
// I need to access to the task index
// not the current value of I
TInterlocked.Add(aResult, I);
end);
aTasks[I].Start;
end;
TTask.WaitForAll(aTasks);
WriteLn(aResult);
end;
预期结果是:
0 +1 +1 2 + 3 = 6
但现在是:
3 + 3 + 3 + 3 = 12