简体   繁体   English

Delphi-使用IFuture从并行编程库中读取大文件

[英]Delphi - Read in a Large File using IFuture from the Parallel Programming Library

I'm reading in some large(ish) excel file which take "ages" to load. 我正在读取一些大的(ish)Excel文件,该文件需要“年龄”才能加载。 I can load it in before I really need to access it. 我可以在确实需要访问它之前将其加载。 So I thought this would be a good use for an IFuture from the Parallel Programming Library. 因此,我认为这对于并行编程库中的IFuture是一个很好的用法。 But I'm not sure how to go about it as all of the "Future" examples only cover simple types such as strings, integers etc. 但是我不确定该怎么做,因为所有“未来”示例仅涉及简单类型,例如字符串,整数等。

Here's the non-parallel code: 这是非并行代码:

xls := TsmXLSFile.Create;
xls.Open(s);

Where "xls" is the Excel object and "s" is a memory stream. 其中“ xls”是Excel对象,“ s”是内存流。

How would a "Future" go about this? “未来”将如何发展? Would I declare xls as... 我可以将xls声明为...

xls := IFuture<TsmXLSFile>

Is this correct. 这个对吗。 If it is then do I need to free it like a normal TsmXLSFile since it's now an interface? 如果是,那么由于它现在是接口,我是否需要像普通的TsmXLSFile一样释放它?

Steve 史蒂夫

Declare a field to get hold of that interface: 声明一个字段以获取该接口:

FXlsFuture: IFuture<TsmXLSFile>;

Add a method to create that future and another one to handle the loaded file: 添加一个方法来创建该未来,并添加另一个方法来处理已加载的文件:

function TForm90.CreateXlsFuture: IFuture<TsmXLSFile>;
begin
  { starts loading }
  Result := TTask.Future<TsmXLSFile>(
    function: TsmXLSFile
    begin
      result := TsmXLSFile.Create;
      result.Open(s);
    end);
end;

procedure TForm90.HandleXlsFuture(AFuture: IFuture<TsmXLSFile>);
var
  xsl: TsmXLSFile;
begin
  xsl := AFuture.Value; { eventually blocks until the file is loaded }
  { do something with the file }
  xsl.Free;
end;

In addition you can query the Status of the future to check if the file is already loaded to avoid blocking. 另外,您可以查询将来的Status以检查文件是否已加载以避免阻塞。

No, you can't do that. 不,你不能那样做。 You would do it more like this: 您将更像这样:

... // this must be a persistant object
  ismXLSFile : IFuture< TsmXLSFile >;
...

// Get started

ismXLSFile := TTask.Future< TsmXLFile > (function : TsmXLFile begin Result := TsmXLFile.Create ); end; );

ismXLSFile.Start;

// Then at some later point
xls := ismXLSFile.Value;

And yes, you do still need to free it. 是的,您仍然需要释放它。 xls is not an interfaced object. xls不是接口的对象。 (ismXLSFile is). (ismXLSFile是)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM