2014-05-17 128 views
-1

我已经找到了网络这个功能,工作得非常好德尔福多线程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它运行的第三方应用程序,这是非常沉重,我的应用程序时间太长,有时挂起 当我从线程调用这个函数它同样需要很长时间来重播 任何建议,使此功能多线程?

+0

怎么能多线程帮助? –

+0

我讨厌这段代码决定命名布尔成功值Handle。 –

+0

我不在乎,如果它只需要exe文件正常工作不会挂起 – dudey

回答

1

代码的问题在于,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; 
+1

这种方法会导致重入问题。 – Torbins