Delphi:如何将当前任务索引访问到ITask数组(TTask)中?

时间:2018-07-18 14:17:25

标签: delphi

我需要将当前任务索引检索到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

0 个答案:

没有答案