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;<br>Problems<br><br>We have experienced problems with WaitForSingleObject in combination with certain applications and we had to finish them with the task manager. Other applications apparently fail to finish correctly and WaitForSingleObject never returns. Appart from that, an application does not respond to events while it waits and this means that for example the forms won't repaint...<br><br>A possible workaround to these problems is checking the status of the launched application (calling GetExitCodeProcess) at intervals (in a timer event).<br><br>uses Forms, Windows, StdCtrls, ExtCtrls;<br><br>type<br>TForm1 = class(TForm)<br> btnExecute: TButton;<br> btnCancel: TButton;<br> Timer1: TTimer;<br> procedure FormCreate(Sender: TObject);<br> procedure btnExecuteClick(Sender: TObject);<br> procedure Timer1Timer(Sender: TObject);<br> procedure btnCancelClick(Sender: TObject);<br> procedure FormCloseQuery(Sender: TObject;<br> var CanClose: Boolean);<br>private<br> proc_info: TProcessInformation;<br> startinfo: TStartupInfo;<br> ExitCode: LongWord;<br>end;<br><br>implementation<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> btnCancel.Enabled := False;<br> Timer1.Enabled := False;<br> Timer1.Interval := 200;<br>end;<br><br>procedure TForm1.btnExecuteClick(Sender: TObject);<br>begin<br> FillChar(proc_info, sizeof(TProcessInformation), 0);<br> FillChar(startinfo, sizeof(TStartupInfo), 0);<br> startinfo.cb := sizeof(TStartupInfo);<br> if CreateProcess(nil, 'c:/windows/notepad.exe', nil,<br> nil, false, CREATE_DEFAULT_ERROR_MODE<br> + NORMAL_PRIORITY_CLASS, nil, nil, startinfo,<br> proc_info) then begin<br> btnExecute.Enabled := False;<br> btnCancel.Enabled := True;<br> Timer1.Enabled := True;<br> end else begin<br> CloseHandle(proc_info.hProcess);<br> Application.MessageBox('Couldn''t execute the '<br> + 'application', 'Error', MB_ICONEXCLAMATION);<br> end;<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>begin<br> Timer1.Enabled := False;<br> if GetExitCodeProcess(proc_info.hProcess, ExitCode)<br> then<br> if ExitCode = STILL_ACTIVE then<br> Timer1.Enabled := True<br> else begin<br> btnCancel.Enabled := False;<br> btnExecute.Enabled := True;<br> CloseHandle(proc_info.hProcess);<br> end<br> else begin<br> btnCancel.Enabled := False;<br> btnExecute.Enabled := True;<br> TerminateProcess(proc_info.hProcess, 0);<br> CloseHandle(proc_info.hProcess);<br> end;<br>end;<br><br>procedure TForm1.btnCancelClick(Sender: TObject);<br>begin<br> Timer1.Enabled := False;<br> if Application.MessageBox('You should try to finish'<br> + ' the application normally.'#13#13'&iquest;Terminate it '<br> + 'anyway?', 'Warning', MB_YESNO + MB_DEFBUTTON2 +<br> MB_ICONQUESTION + MB_TASKMODAL) = ID_YES then begin<br> TerminateProcess(proc_info.hProcess, 0);<br> CloseHandle(proc_info.hProcess);<br> btnCancel.Enabled := False;<br> btnExecute.Enabled := True;<br> end else begin<br> Timer1.Enabled := True;<br> end;<br>end;<br><br>procedure TForm1.FormCloseQuery(Sender: TObject;<br> var CanClose: Boolean);<br>begin<br> if btnCancel.Enabled then begin<br> btnCancelClick(Sender);<br> if btnCancel.Enabled then CanClose := False;<br> end;<br>end;<br>