delphi多线程cmd

时间:2014-05-17 12:41:09

标签: multithreading delphi cmd

我在网上找到了这个功能非常好的

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; 

唯一的问题是,当我运行GetDosOutput时,它会运行非常繁重的第三方应用程序,而且我的应用程序需要太长时间,有时会挂起 当我从线程中调用此函数时,它会花费很长时间来重放 有什么建议使这个功能多线程?

1 个答案:

答案 0 :(得分:1)

代码的问题在于WaitForSingleObject调用显然是在主线程中执行,从而阻止了GUI(至少这是我从你的问题中理解的)。

所以你可以:

  • 将代码包含在.Execute
  • 子类的TThread
  • 使用MsgWaitForMultipleObjects替换呼叫,当Windows消息到达时使用Application.ProcessMessages

你会得到类似的东西:

repeat   
  case MsgWaitForMultipleObjects( 1, PI.hProcess, False, INFINITE, QS_ALLINPUT ) of
    WAIT_OBJECT_0:     Break;
    WAIT_OBJECT_0 + 1: Application.ProcessMessages();
    else Break; // should never happen   
  end; 
until False;