我在一段代码中采用ShellExecute调用一个外部程序,我希望调用进程等到这个外部进程退出后才继续执行,该如何操作呢。
例子:
MainPro:
A
B
ShellExcecute(outerProcess)
C
D
当主进程执行到ShellExcecute时,暂停直到outerProcess执行接收后才执行C、D,该如何操作呢

解决方案 »

  1.   

    用CreateProcess和WaitForSingleObject吧。
    http://community.csdn.net/Expert/topic/3715/3715128.xml?temp=.6361048
      

  2.   

    // Execute the Windows Calculator and pop up
    // a message when the Calc is terminated.
    uses ShellApi;
    ...
    var
      SEInfo: TShellExecuteInfo;
      ExitCode: DWORD;
      ExecuteFile, ParamString, StartInString: string;
    begin
      ExecuteFile:='c:\Windows\Calc.exe';
      
      FillChar(SEInfo, SizeOf(SEInfo), 0);
      SEInfo.cbSize := SizeOf(TShellExecuteInfo);
      with SEInfo do begin
        fMask := SEE_MASK_NOCLOSEPROCESS;
        Wnd := Application.Handle;
        lpFile := PChar(ExecuteFile);
    {
    ParamString can contain the
    application parameters.
    }
    // lpParameters := PChar(ParamString);
    {
    StartInString specifies the
    name of the working directory.
    If ommited, the current directory is used.
    }
    //  lpDirectory := PChar(StartInString);
        nShow := SW_SHOWNORMAL;
      end;
      if ShellExecuteEx(@SEInfo) then begin
        repeat
          Application.ProcessMessages;
          GetExitCodeProcess(SEInfo.hProcess, ExitCode);
        until (ExitCode <> STILL_ACTIVE) or
       Application.Terminated;
        ShowMessage('Calculator terminated');
      end
      else ShowMessage('Error starting Calc!');
    end;