Mega Code Archive

 
Categories / Delphi / Examples
 

WinExecAndWait

Title: WinExecAndWait Question: How to start an application and wait for it to be terminated? Answer: Here's a function that is called in the same way as WinExec but will wait for the called task to terminate. The only difference is that if the function is successful. function WinExecAndWait(Path: PChar; Visibility: Word): Word; var InstanceID: THandle; Msg: TMsg; begin InstanceID := WinExec(Path, Visibility); if InstanceID error } WinExecAndWait := InstanceID else repeat while PeekMessage(Msg, 0, 0, 0, pm_Remove) do begin if Msg.Message = wm_Quit then Halt(Msg.WParam); TranslateMessage(Msg); DispatchMessage(Msg); end; until GetModuleUsage(InstanceID) = 0; WinExecAndWait := 0; end;