Launching the application and waiting for it to close. It is possible to specify the maximum waiting time, after which the function will continue to work without waiting for the termination of the running application. Hidden launch of applications without the appearance of windows, both console and windowed.
1 2 3 4 5 6 7 8 9 10 11 12 |
var path_params, workDir: WideString; sec: Integer; showWin: Boolean; begin path_params:= 'ping.exe 8.8.8.8'; workDir:= ''; sec:= 5; showWin:= True; execAndWait(path_params, workDir, sec, showWin); end; |
<! - more -> Options
path_params - path to the application with parameters
workDir - application working directory
sec - maximum time to wait for close in seconds (0 - do not wait)
showWin - show the window of the application being launched or hide
Result
True if the application was launched successfully, False if not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
function execAndWait(appPath_Params, workDir: WideString; sec: Integer; showWin: Boolean): Boolean; var si : TStartupInfo; i:Integer; procID : TProcessInformation; path_params, wdir: PChar; begin if appPath_Params <> '' then path_params:= PChar(appPath_Params) else path_params:= nil; if workDir <> '' then wdir:= PChar(workDir) else wdir:= nil; zeroMemory(@si,sizeof(si)); si.cb := sizeof(si); if showWin = True then si.wShowWindow:= 1 else si.wShowWindow:= 0; si.dwFlags := STARTF_USESHOWWINDOW; zeroMemory(@procID,sizeof(procID)); if not CreateProcess(nil, path_params, nil, nil, false, 0, nil, wdir, si, procID ) then begin Result:= False; exit; end else begin Result:= True; if sec = 0 then exit; i := 0; while i < sec * 20 do begin if waitForSingleObject(procID.hProcess, 0) = 0 then begin closehandle(procID.hProcess); closeHandle(procID.hThread); Break; end; sleep(50); Application.ProcessMessages; inc(i); end; end; end; |