获取非阻塞CommandLine输出 [英] Get non blocking CommandLine output

查看:118
本文介绍了获取非阻塞CommandLine输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此函数从 DelphiDabbler 获取delphi中的dos输出。但是如果进程需要很长时间(例如:gammu getussd命令),它会导致我的应用程序暂时没有响应。

如何避免这种情况?

I use this function to get dos output in delphi from DelphiDabbler. But if the process takes a long time (for example: gammu getussd command) it causes my application to be not responding for a while.
How to avoid this?

function GetDosOutput(CommandLine: string; Work: string = 'C:\'): string;
var
  SA: TSecurityAttributes;
  SI: TStartupInfo;
  PI: TProcessInformation;
  StdOutPipeRead, StdOutPipeWrite: THandle;
  WasOK: Boolean;
  Buffer: array[0..255] of AnsiChar;
  BytesRead: Cardinal;
  WorkDir: string;
  Handle: Boolean;
begin
  Result := '';
  with SA do begin
    nLength := SizeOf(SA);
    bInheritHandle := True;
    lpSecurityDescriptor := nil;
  end;
  CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
  try
    with SI do
    begin
      FillChar(SI, SizeOf(SI), 0);
      cb := SizeOf(SI);
      dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
      wShowWindow := SW_HIDE;
      hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
      hStdOutput := StdOutPipeWrite;
      hStdError := StdOutPipeWrite;
    end;
    WorkDir := Work;
    Handle := CreateProcess(nil, PChar('cmd.exe /C ' + CommandLine),
                            nil, nil, True, 0, nil,
                            PChar(WorkDir), SI, PI);
    CloseHandle(StdOutPipeWrite);
    if Handle then
      try
        repeat
          WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
          if BytesRead > 0 then
          begin
            Buffer[BytesRead] := #0;
            Result := Result + Buffer;
          end;
        until not WasOK or (BytesRead = 0);
        WaitForSingleObject(PI.hProcess, INFINITE);
      finally
        CloseHandle(PI.hThread);
        CloseHandle(PI.hProcess);
      end;
  finally
    CloseHandle(StdOutPipeRead);
  end;
end;

作者:Joe Donth

Author: Joe Donth

推荐答案

您可以使用一个简单的线程来获取OnTerminate事件的结果,该事件将在主线程的上下文中运行,因此不需要同步:

You might use a simple thread for this and get the result in the OnTerminate Event, which will run in the context of the main thread, so that no furthor synchronisation is needed:

Type
  TMyThread = Class(TThread)
  private
    FCmd: String;
    FResult: String;
  protected
    Procedure Execute; override;
  public
    Constructor Create(const cmd: String;Notity:TNotifyEvent);
    Property Result: String Read FResult;
  End;
  { TMyTHread }

constructor TMyThread.Create(const cmd: String;Notity:TNotifyEvent);
begin
  inherited Create(false);
  FCmd := cmd;
  FreeOnTerminate := True;
  OnTerminate := Notity;
end;

procedure TMyThread.Execute;
begin
  inherited;
  FResult := GetDosOutput(FCmd);
end;

procedure TForm3.ThreadTerminated(Sender: TObject);
begin
  Memo1.Lines.Text := TMyThread(Sender).Result;
end;

procedure TForm3.Button1Click(Sender: TObject);
begin
  TMyThread.Create('DIR',ThreadTerminated);
end;

这篇关于获取非阻塞CommandLine输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆