Board index » delphi » Execute a program and wait until it finished.

Execute a program and wait until it finished.

Hi

How do I execute a program from within a Delphi application and have my
program wait until it is finished - the program is closed ?.

Best Regards

Johnny Hansen

 

Re:Execute a program and wait until it finished.


hi !
        InstallShield !!!

@++
    cpmi...@wanadoo.fr

Re:Execute a program and wait until it finished.


Hej

Procedure ExecAndWait;
var
  Return : Integer;
  StartupInfo : TStartupInfo;
  ProcessInfo : TProcessInformation;
begin
  FillChar(Startupinfo,SizeOf(TstartupInfo),#0);
  With StartupInfo Do
    Begin
      cb:=SizeOf(TstartupInfo);
      dwFlags:=STARTF_USESIZE or StartF_UsePosition;
    End;
  If CreateProcess(Nil,PChar('Myfile.exe'), nil,nil,False,Create_New_Console
or
                  Normal_Priority_Class,Nil,Nil,Startupinfo,ProcessInfo)
Then
  Repeat
    GetExitCodeProcess(ProcessInfo.hProcess,Return);
    Application.ProcessMessages;
  Until Return<>Still_Active;
End;

--
Med venlig hilsen

Ole Mortensen, Primeware

http://www.primeware.dk

Quote
Johnny Hansen <j...@sfk.dk> wrote in message

news:4CvY2.28$Cc4.129@news.get2net.dk...
Quote
> Hi

> How do I execute a program from within a Delphi application and have my
> program wait until it is finished - the program is closed ?.

> Best Regards

> Johnny Hansen

Re:Execute a program and wait until it finished.


In article <4CvY2.28$Cc4....@news.get2net.dk>, j...@sfk.dk says...
Quote
> How do I execute a program from within a Delphi application and have my
> program wait until it is finished - the program is closed ?.

* Launch the program using CreateProcess().
* Close the hThread in the returned TProcessInformation structure.
* Pass the hProcess in the returned TP... structure to a simple process-
done thread (shown below), along with an event handler in your main
program.
* When the process terminates, the passed event handler will be
triggered.

type
  TProcDoneThread = class(TThread)
    ProcHandle : THandle;
    Constructor Create(vProcHandle: THandle; OnTerm: TNotifyEvent);
    procedure Execute; override;
  end;

constructor TProcDoneThread.Create(vProcHandle: THandle;
  OnTerm: TNotifyEvent);
begin
  Inherited Create(False);
  ProcHandle      := vProcHandle;
  OnTerminate     := OnTerm;
  FreeOnTerminate := True;
end;

procedure TProcDoneThread.execute;
begin
  WaitForSingleObject(ProcHandle, INFINITE);
  CloseHandle(ProcHandle);
end;

--
Neil J. Rubenking
Contributing Technical Editor
PC Magazine

Other Threads