Waiting till an application ends<br>WaitForSingleObject<br><br>If we ever need to run an external application and wait until it terminates, then we would have to do without ShellExecute and resort to more basic functions, like CreateProcess, WaitForSingleObject and CloseHandle.<br><br>uses Forms, Windows;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> proc_info: TProcessInformation;<br> startinfo: TStartupInfo;<br> ExitCode: longword;<br>begin<br> // Initialize the structures<br> FillChar(proc_info, sizeof(TProcessInformation), 0);<br> FillChar(startinfo, sizeof(TStartupInfo), 0);<br> startinfo.cb := sizeof(TStartupInfo);<br><br> // Attempts to create the process<br> if CreateProcess('c:/windows/notepad.exe', nil, nil,<br> nil, false, NORMAL_PRIORITY_CLASS, nil, nil,<br> startinfo, proc_info) <> False then begin<br> // The process has been successfully created<br> // No let's wait till it ends...<br> WaitForSingleObject(proc_info.hProcess, INFINITE);<br> // Process has finished. Now we should close it.<br> GetExitCodeProcess(proc_info.hProcess, ExitCode); // Optional<br> CloseHandle(proc_info.hThread);<br> CloseHandle(proc_info.hProcess);<br> Application.MessageBox(<br> PChar(Format('Notepad finished! (Exit code=%d)', [ExitCode])),<br> 'Info', MB_ICONINFORMATION);<br> end else begin<br> // Failure creating the process<br> Application.MessageBox('Couldn''t execute the '<br> + 'application', 'Error', MB_ICONEXCLAMATION);<br> end;//if<br>end;