delphi - Delphi 无法在具有对象引用的任务中执行过程
问题描述
我有一个简单的类,它具有以下接口实现。
注意:TPolyBase
是一个抽象类,TPolyResult
是一个array 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.
解决方案
您的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.Free
到f.Free
完成处理程序中。
推荐阅读
- google-cloud-storage - 将文件从 GCS 复制到 AWS S3 时如何设置 acl?
- javascript - JSON.parse 错误:位置 1 处的字符无效
- pandas - 使用 Pandas to_hdf 时,是否可以为不规则张量指定 vlen special_dtype / vlarray 的列数据类型?
- laravel - Uncaught SyntaxError: Unexpected identifier in import Echo from 'laravel-echo' js file present in resources/js/bootstrap.js
- for-loop - 如果有scanf,为什么在循环时会得到重复的输出?
- amazon-web-services - AWS VPC 终端节点接口是否需要 Internet 网关
- r - 如何将 ggplots 列表打印到动画 GIF?
- python - 在 IPython 中生成脚本的帮助字符串
- java - 如何使用 SQL 中的变量删除表中的行
- visual-studio-2017 - 发送邮递员 GET 请求后,未命中 Visual Studio Web API 控制器中的断点