Delphi无法在Task中使用对象引用执行过程

问题描述 投票:2回答:1

我有一个简单的类,具有以下接口实现。

注意:TPolyBase是一个抽象类,TPolyResultarray of double;看到他们的代码并不重要,这与此无关。

//INTERFACE

type
  TPolynomialList = class
    strict private
      FPolynomialList: TObjectList<TPolyBase>;
      FResult: TList<TPolyResult>;
      FCanGet: boolean;
      function GetResult: TList<TPolyResult>;
      procedure DoSolve;
    public
      constructor Create(PolynomialList: TObjectList<TPolyBase>);
      destructor Destroy; override;

      procedure SolvePolynomials(CompletionHandler: TProc);
      property Solutions: TList<TPolyResult> read GetResult;
  end;

//IMPLEMENTATION

constructor TPolynomialList.Create(PolynomialList: TObjectList<TPolyBase>);
begin
  FPolynomialList := PolynomialList;
  FResult := TList<TPolyResult>.Create;
  FCanGet := false;
end;

destructor TPolynomialList.Destroy;
begin
  FResult.Free;
  inherited;
end;

procedure TPolynomialList.DoSolve;
var
  i: integer;
begin
  for i := 0 to FPolynomialList.Count - 1 do
    FResult.Add(FPolynomialList[i].GetSolutions);

  FCanGet := true;
end;

function TPolynomialList.GetResult: TList<TPolyResult>;
begin
  if FCanGet = false then
    raise TEquationError.Create('You must solve the equation first!');

  Result := FResult;
end;

procedure TPolynomialList.SolvePolynomials(CompletionHandler: TProc);
begin
    TTask.Run(procedure
              var
                ex: TObject;
              begin
                try
                  DoSolve;
                  TThread.Synchronize(nil, procedure
                                           begin
                                             CompletionHandler;
                                           end);
                except
                  on E: Exception do
                    begin
                      ex := AcquireExceptionObject;
                      TThread.Synchronize(nil, procedure
                                           begin
                                             Writeln( (ex as Exception).Message );  
                                           end);
                    end;
                end;
              end);
end;

此类将对象列表作为输入,它有一个名为FResult的内部重要字段,用于将结果提供给用户。只有当SolvePolynomials方法完成了他的工作时,才可以从getter访问它。


问题在于SolvePolynomials。我显示的代码使用了一个任务,因为对象列表的大小可能非常大,我不想冻结UI。为什么我总是在任务代码中获得访问冲突?

请注意,以下代码工作正常,但这不是我想要的,因为如果我输入15000程序冻结几秒钟。

procedure TPolynomialList.SolvePolynomials(CompletionHandler: TProc);
begin
  DoSolve;
  CompletionHandler;
end;

FPolynomialList变量可能是一个问题吗?如果你看看我的班级,唯一“从外面采取”的是TObjectList<TPolyBase>,因为在构造函数中我只是帮助引用(我想避免复制ok 15k项目)。所有其他变量都不与任何共享。

我在很多书中都看到过像“Delphi High Performance”这样的书,这个书有一个叫做内部“慢”方法的任务很好但是在这种情况下可能会有那些搞乱的东西。任何的想法?


这是我用作测试的代码:

var
 a: TObjectList<TPolyBase>;
 i, j: integer;
 f: TPolynomialList;
 s: string;

 function GetRandom: integer;
 begin
   Result := (Random(10) + 1);
 end;

begin
a := TObjectList<TPolyBase>.Create(true);
 try

   for i := 0 to 15000 do
     begin
       a.Add({*Descendant of TPolyBase*})
     end;


   f := TPolynomialList.Create(a);
   try
     f.SolvePolynomials(procedure
                        var
                          i, j: integer;
                        begin    
                          for i := 0 to f.Solutions.Count - 1 do
                            begin
                              for j := Low(f.Solutions[i]) to High(f.Solutions[i]) do
                                Writeln({output the results...})
                            end;  
                        end);
   finally
     f.Free;
   end;

 finally
   a.Free;
 end;

end.
delphi delphi-10.2-tokyo
1个回答
5
投票

你的SolvePolynomials方法将求解委托给另一个线程,并在该线程完成其任务之前返回。当该任务线程正在运行时,它所运行的所有数据仍然有效。但是,在您的代码中,您在SolvePolynomials退出后立即释放必要的对象实例 - 当您的任务仍在运行时,因此错误。

您必须将这些对象的释放移动到完成处理程序中。

基本上,您的代码简化如下:

type
  TPolynomialList = class
  public
    destructor Destroy; override;
    procedure DoSolve;
    procedure SolvePolynomials(CompletionHandler: TProc);
  end;

destructor TPolynomialList.Destroy;
begin
  Writeln('Destroyed');
  inherited;
end;

procedure TPolynomialList.DoSolve;
begin
  Writeln('Solving');
end;

procedure TPolynomialList.SolvePolynomials(CompletionHandler: TProc);
begin
  TTask.Run(
  procedure
  begin
    try
      DoSolve;
      TThread.Synchronize(nil,
        procedure
        begin
          CompletionHandler;
        end);
    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  end);
end;

procedure Test;
var
  f: TPolynomialList;
begin
  f := TPolynomialList.Create;
  try
    f.SolvePolynomials(
      procedure
      begin
        Writeln('Solved');
      end);
  finally
    f.Free;
  end;
end;

如果你运行它输出将是:

Destroyed
Solving
Solved

但是,如果您将变量释放到完成处理程序中,则执行顺序将是正确的。

procedure Test;
var
  f: TPolynomialList;
begin
  f := TPolynomialList.Create;
  f.SolvePolynomials(
    procedure
    begin
      Writeln('Solved');
      f.Free;
    end);
end;

Solving
Solved
Destroyed

对于您的代码,这意味着将a.Freef.Free都移动到完成处理程序中。

© www.soinside.com 2019 - 2024. All rights reserved.