如何将循环中的上下文传递到Delphi中的TTask.IFuture?

时间:2017-12-08 02:46:55

标签: delphi delphi-10.2-tokyo

我有一组任务要执行它们在一个数组中。我想遍历数组并为每个数组启动一个任务。

以下示例很简单(计算方块)来演示问题。

program FutureSquares;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  System.Threading,
  System.Classes;
const
  nums: array[0..9] of Integer = (1,2,3,4,5,6,7,8,9,10);
var
  i, contextIndex: Integer;
  f: array[0..9] of ITask;
  answer, futureAnswer: Integer;
  matchDisplay: string;
  futureFunc: TFunc<Integer>;
begin

  try
    for i := Low(f) to High(f) do
    begin
      contextIndex := i;

      futureFunc := function: Integer
        begin
          Sleep(Random(1000));
          Result := nums[contextIndex]*nums[contextIndex]; // trying  because a reference to it is captured. How to capture the actual value?
        end;

      f[i] := TTask.Future<Integer>(futureFunc);
      f[i].Start;
    end;

    //verify results in sequential manner
    for i := Low(f) to High(f) do
    begin
      answer := nums[i]*nums[i];
      futureAnswer := IFuture<Integer>(f[i]).Value;
      if futureAnswer = answer then
        matchDisplay := 'match'
      else
        matchDisplay := 'MISMATCH';
      writeln(Format('%d * %d = %d. Future<Integer> = %d - %s', [nums[i],nums[i],answer, futureAnswer, matchDisplay]));
    end;

    readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

程序的输出如下:

1 * 1 = 1. Future<Integer> = 16 - MISMATCH
2 * 2 = 4. Future<Integer> = 100 - MISMATCH
3 * 3 = 9. Future<Integer> = 100 - MISMATCH
4 * 4 = 16. Future<Integer> = 100 - MISMATCH
5 * 5 = 25. Future<Integer> = 100 - MISMATCH
6 * 6 = 36. Future<Integer> = 100 - MISMATCH
7 * 7 = 49. Future<Integer> = 100 - MISMATCH
8 * 8 = 64. Future<Integer> = 100 - MISMATCH
9 * 9 = 81. Future<Integer> = 100 - MISMATCH
10 * 10 = 100. Future<Integer> = 100 - match

我如何实现目标?

我注意到TTask.Future不允许我向它传递一个有用的上下文。

1 个答案:

答案 0 :(得分:3)

您需要捕获匿名方法中的值而不是变量。最简单的方法是将匿名方法放在一个独立的函数中,并传入您想要捕获的索引。这样,将捕获该值而不是变量。

println(test1.filter(_.toLowerCase.startsWith("z")).length)

然后,当您执行futureFunc的分配时,您的代码将如下所示:

function CaptureFuture(const aTheIndex: Integer): TFunc<Integer>;
begin
  Result := function: Integer
    begin
      Sleep(Random(1000));
      Result := nums[aTheIndex]*nums[aTheIndex]; // trying  because a reference to it is captured. How to capture the actual value?
    end;
end;

这将为您提供所需的结果:

for i := Low(f) to High(f) do
begin
  futureFunc := CaptureFuture(i);

  f[i] := TTask.Future<Integer>(futureFunc);
  f[i].Start;
end;

有关变量与值捕获的更多信息,请参阅此问题: Anonymous methods - variable capture versus value capture