How to start a application and wait for its execution in delphi?
How to start a application and wait for its execution in delphi?
if you want start a external exe application, and wait until it finished , the demo code listed as below:
Uses ..., ShellAPI; procedure StartAndWait( Name: PChar ); var ProcInfo: PShellExecuteInfo; begin GetMem( ProcInfo, SizeOf( ProcInfo^ ) ); with ProcInfo^ do begin Wnd := Application.Handle; cbSize := SizeOf( ProcInfo^ ); lpFile := PChar( Name ); lpParameters := nil; lpVerb := 'open'; nShow := SW_SHOW; fMask := SEE_MASK_DOENVSUBST or SEE_MASK_NOCLOSEPROCESS; end; try Win32check( ShellExecuteEx( ProcInfo ) ); while not Application.Terminated and ( WaitForSingleObject( ProcInfo.hProcess, 100 ) = WAIT_TIMEOUT ) do Application.ProcessMessages; finally if ProcInfo.hProcess <> 0 then CloseHandle( ProcInfo.hProcess ); Dispose( ProcInfo ); end; end; procedure TForm1.Button1Click(Sender: TObject); begin ( Sender as TControl ).Enabled := false; StartAndWait( 'notepad.exe' ); ( Sender as TControl ).Enabled := true; end;