Delphi 使用计时器在线程和对象中进行管道传输

Delphi 使用计时器在线程和对象中进行管道传输,delphi,pipeline,delphi-xe6,Delphi,Pipeline,Delphi Xe6,我的应用程序有一个对象,它监视它创建的命令提示符进程的输出和错误。我有两个实现,一个在线程中运行,另一个使用TTimer组件运行,该组件在再次调用OnTimer事件之前等待50毫秒 线程方法在任何时候都不会从进程返回输出/错误文本,而计时器方法确实返回输出/错误文本,但不一定以正确的顺序返回(我怀疑管道中存在累积,因此我希望使用线程来减少管道检查之间的时间) 有人建议我尽可能频繁地监控输出/错误管道,但这不是一个巨大的CPU消耗吗 计时器代码: procedure TCmdPipeliner.O

我的应用程序有一个对象,它监视它创建的命令提示符进程的输出和错误。我有两个实现,一个在线程中运行,另一个使用TTimer组件运行,该组件在再次调用OnTimer事件之前等待50毫秒

线程方法在任何时候都不会从进程返回输出/错误文本,而计时器方法确实返回输出/错误文本,但不一定以正确的顺序返回(我怀疑管道中存在累积,因此我希望使用线程来减少管道检查之间的时间)

有人建议我尽可能频繁地监控输出/错误管道,但这不是一个巨大的CPU消耗吗

计时器代码:

procedure TCmdPipeliner.OnReadTimer(Sender: TObject);
var
  ReadPipeResult, ErrorPipeResult : string;
begin

  // Read in pipe data
  ReadPipeResult := Self.ReadPipeContent;
  ErrorPipeResult := Self.ErrorPipeContent;

  if (ReadPipeResult <> '') and (Assigned(Self.FOnPipeRead)) then Self.FOnPipeRead(Self, ReadPipeResult);
  if (ErrorPipeResult <> '') and (Assigned(Self.FOnPipeError)) then Self.FOnPipeError(Self, ErrorPipeResult);

end;
过程TCmdPipeliner.OnReadTimer(发送方:TObject);
变量
ReadPipeResult,ErrorPipeResult:字符串;
开始
//读入管道数据
ReadPipeResult:=Self.ReadPipeContent;
ErrorPipeResult:=Self.ErrorPipeContent;
如果是(ReadPipeResult“”)和(赋值为(Self.FOnPipeRead)),则Self.FOnPipeRead(Self,ReadPipeResult);
如果是(ErrorPipeResult“”)和(Assigned(Self.FOnPipeError)),则是Self.FOnPipeError(Self,ErrorPipeResult);
结束;
线程代码:

procedure TCmdPipeliner.Execute;
var
  ReadPipeResult, ErrorPipeResult : string;
begin

  while not(Self.Terminated) do
    begin

      // Read in pipe data
      ReadPipeResult := Self.ReadPipeContent;
      ErrorPipeResult := Self.ErrorPipeContent;

      if (ReadPipeResult <> '') and (Assigned(Self.FOnPipeRead)) then Self.FOnPipeRead(Self, ReadPipeResult);
      if (ErrorPipeResult <> '') and (Assigned(Self.FOnPipeError)) then Self.FOnPipeError(Self, ErrorPipeResult);

      sleep(1);

    end;
  {end while}

end;
过程TCmdPipeliner.Execute;
变量
ReadPipeResult,ErrorPipeResult:字符串;
开始
而不是(自我终止)做什么
开始
//读入管道数据
ReadPipeResult:=Self.ReadPipeContent;
ErrorPipeResult:=Self.ErrorPipeContent;
如果是(ReadPipeResult“”)和(赋值为(Self.FOnPipeRead)),则Self.FOnPipeRead(Self,ReadPipeResult);
如果是(ErrorPipeResult“”)和(Assigned(Self.FOnPipeError)),则是Self.FOnPipeError(Self,ErrorPipeResult);
睡眠(1);
结束;
{结束时}
结束;
我曾尝试使用“同步”,但运气不佳

  if (ErrorPipeResult <> '') then
    if (Assigned(Self.FOnPipeError)) then
      Synchronize(procedure
        begin
          Self.FOnPipeError(Self, ErrorPipeResult);
        end);

  if (ReadPipeResult <> '') then
    if (Assigned(Self.FOnPipeRead)) then
      Synchronize(procedure
        begin
          Self.FOnPipeRead(Self, ReadPipeResult);
        end);
如果(ErrorPipeResult“”),则
如果(赋值(Self.error))则
同步(程序)
开始
Self.FOnPipeError(Self,ErrorPipeResult);
(完),;
如果(ReadPipeResult“”),则
如果(赋值(Self.FOnPipeRead)),则
同步(程序)
开始
Self.FOnPipeRead(Self,ReadPipeResult);
(完),;

你应该写1000次“一个线程中的Application.ProcessMessages是邪恶的!”:o)@SirRufo我知道这不太好,哈哈!但我想先有一个工作的解决方案,它是调试遗留下来的!请注意这个(德国论坛链接),您可以使用异步IO来避免轮询。