Delphiran - Delphi Tips
Question :
How can I launch an application and wait until it finishes ?

Answer :
procedure ExecAndWaitProcess(ExecPath, Parameters:String);
Var
     SUI: TSTARTUPINFO;
     PIN: PROCESS_INFORMATION;
     Path1, Path2: String;
     ExitCode:DWORD;

begin

with SUI do begin
      cb:=Sizeof(SUI);
      lpReserved:=Nil;
      lpDesktop:=Nil;
      lpTitle:=Nil;
      dwX:=0;
      dwY:=0;
      dwXSize:=0;
      dwYSize:=0;
      dwXCountChars:=0;
      dwYCountChars:=0;
      dwFillAttribute:=0;
      dwFlags:=0;
      wShowWindow:=0;
      cbReserved2:=0;
      lpReserved2:=Nil;
 end;

 Path1:=ExecPath;
 Path2:=Path1+' '+Parameters;

 // Arrancar el EmbConfig
 if CreateProcess(
    PChar(Path1),  // pointer to name of executable module
    PChar(Path2),  // pointer to command line string
    Nil,           // pointer to process security attributes
    Nil,           // pointer to thread security attributes
    False,         // handle inheritance flag
    CREATE_DEFAULT_ERROR_MODE, // creation flags
    Nil,                       // pointer to new environment block
    PChar(ExtractFilePath(Path1)), // pointer to current directory name
    SUI,                           // pointer to STARTUPINFO
    PIN                            // pointer to PROCESS_INFORMATION
  )  then begin                    // Process is running
      Self.Enabled:=false;  // I Disable my own process, optional
      // Some people prefer to use INFINITE, I wait 3 seconds and process my messages
      // as you prefer
      While (WaitForSingleObjectEx(PIN.hProcess, 3, False) = WAIT_TIMEOUT) do
      begin
           Application.ProcessMessages;
      end;
      Self.Enabled:=True; // Enabled my own process
      GetExitCodeProcess(PIN.hProcess, ExitCode);
      // ExitCode is the process exit code, obvious
      If ExitCode=0 then begin // 0 or any other value you want to check for
           // whatever
      end;
 end
 else begin // CreateProcess has failed!
      // do as needed
 end;
end;
 
 

 
 
© All rights reserved 1999 BuyPin Software