Multithreading delphi多线程cmd

Multithreading delphi多线程cmd,multithreading,delphi,cmd,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.

我在网上找到了这个功能,效果非常好

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时,它会运行第三方应用程序,该应用程序非常繁重,而且我的应用程序耗时太长,有时会挂起 当我从线程调用这个函数时,重播同样需要很长时间
有没有建议将此函数设置为多线程?

代码的问题在于,
WaitForSingleObject
调用显然是在主线程中执行的,因此阻塞了GUI(至少我从您的问题中了解到了这一点)

所以你可以:

  • TThread
    的子类的
    .Execute
    中包装代码
  • 将调用替换为
    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;

多线程有什么帮助?我讨厌这段代码决定命名布尔成功值句柄。我不在乎它是否需要时间,只是希望exe正常工作而不是挂起。多线程有什么帮助?如何将单个任务拆分为多个线程?这种方法可能会导致重入问题。