gabr 对另一个问题的回答显示了使用 Parallel.Pipeline 进行数据处理的示例。
目前我需要知道 Pipeline 何时启动以及其所有阶段何时完成。我阅读了其他 gabr 对此问题的回答如何在 OmniThreadLibrary 中监控管道阶段?. 我试着这样做(根据答案修改):
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, superobject,
OtlCommon, OtlCollections, OtlParallel, OtlComm, OtlTask, ExtCtrls;
const
WM_STARTED = WM_USER;
WM_ENDED = WM_USER + 1;
type
TForm1 = class(TForm)
btnStart: TButton;
btnStop: TButton;
lbLog: TListBox;
procedure btnStartClick(Sender: TObject);
procedure btnStopClick(Sender: TObject);
private
FCounterTotal: IOmniCounter;
FCounterProcessed: IOmniCounter;
FIsBusy: boolean;
FPipeline: IOmniPipeline;
procedure WMStarted(var msg: TOmniMessage); message WM_STARTED;
procedure WMEnded(var msg: TOmniMessage); message WM_ENDED;
strict protected
procedure Async_Files(const input, output: IOmniBlockingCollection; const task: IOmniTask);
procedure Async_Parse(const input: TOmniValue; var output: TOmniValue);
procedure Async_JSON(const input, output: IOmniBlockingCollection; const task: IOmniTask);
end;
var
Form1: TForm1;
procedure GetJSON_(const AData: PChar; var Output: WideString); stdcall; external 'my.dll';
implementation
uses IOUtils;
{$R *.dfm}
procedure TForm1.Async_Files(const input, output: IOmniBlockingCollection; const task: IOmniTask);
var
i, cnt: integer;
f: string;
begin
while not input.IsCompleted do begin
task.Comm.Send(WM_STARTED); // message is sent once every 1 min
cnt := 0;
for f in TDirectory.GetFiles(ExtractFilePath(Application.ExeName), '*.txt') do
begin
output.TryAdd(f);
Inc(cnt);
Sleep(1000); // simulate a work
end;
FCounterTotal.Value := cnt;
// I need to continously check a specified folder for new files, with
// a period of 1 minute (60 sec) for an unlimited period of time.
i := 60;
repeat
Sleep(1000); // Check if we should stop every second (if Stop button is pushed)
if input.IsCompleted then Break;
dec(i);
until i < 0;
end;
end;
procedure TForm1.Async_Parse(const input: TOmniValue; var output: TOmniValue);
var
sl: TStringList;
ws: WideString;
begin
sl := TStringList.Create;
try
sl.LoadFromFile(input.AsString);
GetJSON_(PChar(sl.Text), ws); // output as ISuperObject --- DLL procedure
output := SO(ws);
// TFile.Delete(input.AsString); // For testing purposes only - Continue without Deleting Processed File
finally
sl.Free;
end;
end;
procedure TForm1.Async_JSON(const input, output: IOmniBlockingCollection; const task: IOmniTask);
var
value: TOmniValue;
JSON: ISuperObject;
cnt: integer;
begin
for value in input do begin
JSON := value.AsInterface as ISuperObject;
// do something with JSON
cnt := FCounterProcessed.Increment;
if FCounterTotal.Value = cnt then
task.Comm.Send(WM_ENDED); // !!! message is not sent
end;
end;
//
procedure TForm1.btnStartClick(Sender: TObject);
begin
btnStart.Enabled := False;
FCounterTotal := CreateCounter(-1);
FCounterProcessed := CreateCounter(0);
FPipeline := Parallel.Pipeline
.Stage(Async_Files, Parallel.TaskConfig.OnMessage(Self))
.Stage(Async_Parse)
.Stage(Async_JSON, Parallel.TaskConfig.OnMessage(Self))
.Run;
end;
procedure TForm1.btnStopClick(Sender: TObject);
begin
if Assigned(FPipeline) then begin
FPipeline.Input.CompleteAdding;
FPipeline := nil;
end;
btnStart.Enabled := True;
end;
//
procedure TForm1.WMEnded(var msg: TOmniMessage);
begin
FIsBusy := False;
lbLog.ItemIndex := lbLog.Items.Add(Format('%s - Pipeline stage 3 ended', [DateTimeToStr(Now)]));
end;
procedure TForm1.WMStarted(var msg: TOmniMessage);
begin
FIsBusy := True;
lbLog.ItemIndex := lbLog.Items.Add(Format('%s - Pipeline stage 1 starting', [DateTimeToStr(Now)]));
end;
end.
task.Comm.Send(WM_STARTED)
一切正常,但该行永远task.Comm.Send(WM_ENDED)
不会执行。我怎么知道最后一个阶段何时完成?正确的方法是什么?