Ian Branch 127 Posted March 13, 2019 (edited) Hi Team, I have a small App that lets users call other Apps using the following borrowed code example for MyApp1 & 2... procedure TMainForm.btnMyApp1Click(Sender: TObject); var SEInfo: TShellExecuteInfo; ExitCode: DWORD; ExecuteFile: string; begin lMyApp1Open := True; Cursor := crHourGlass; btnMyApp1.Color := clLime; // Standard is clBtnFace ExecuteFile := sAppsFilePath + '\MyApp1.exe'; FillChar(SEInfo, SizeOf(SEInfo), 0); SEInfo.cbSize := SizeOf(TShellExecuteInfo); with SEInfo do begin fMask := SEE_MASK_NOCLOSEPROCESS; Wnd := Application.Handle; lpFile := PChar(ExecuteFile); nShow := SW_SHOWNORMAL; end; Cursor := crDefault; if ShellExecuteEx(@SEInfo) then begin repeat Application.ProcessMessages; GetExitCodeProcess(SEInfo.hProcess, ExitCode); until (ExitCode <> STILL_ACTIVE) or Application.Terminated; end else ShowMessage('Error starting MyApp1!'); btnMyApp1.Color := clBtnFace; // Standard is clBtnFace lMyApp1Open := False; end; // procedure TMainForm.btnMyApp2Click(Sender: TObject); var SEInfo: TShellExecuteInfo; ExitCode: DWORD; ExecuteFile: string; begin lMyApp2Open := True; Cursor := crHourGlass; btnMyApp2.Color := clLime; // Standard is clBtnFace ExecuteFile := sAppsFilePath + '\MyApp2.exe'; FillChar(SEInfo, SizeOf(SEInfo), 0); SEInfo.cbSize := SizeOf(TShellExecuteInfo); with SEInfo do begin fMask := SEE_MASK_NOCLOSEPROCESS; Wnd := Application.Handle; lpFile := PChar(ExecuteFile); nShow := SW_SHOWNORMAL; end; Cursor := crDefault; if ShellExecuteEx(@SEInfo) then begin repeat Application.ProcessMessages; GetExitCodeProcess(SEInfo.hProcess, ExitCode); until (ExitCode <> STILL_ACTIVE) or Application.Terminated; end else ShowMessage('Error starting MyApp2!'); btnMyApp2.Color := clBtnFace; // Standard is clBtnFace lMyApp2Open := False; end; The issue is that if both MyApp1 & MyApp2 apps are called and MyApp1 closes, it does actually close, it appears to stay open in my front end app, the buttons stay colored, until MyApp2 closes. Then both buttons return to normal color. This is an area I have no knowledge of. Can someone advise/suggest what is not happening and how it can be corrected please? Be greatly appreciated. Regards & TIA, Ian Edited March 13, 2019 by Sherlock Used correct code tags Share this post Link to post
David Heffernan 2345 Posted March 13, 2019 That's because your button event handlers run busy message loops waiting for the child processes to finish. Your mistake is to run those busy loops in the main thread. You should consider running those loops in dedicated threads, and obviously remove the message processing. You code leaks the process handles and has other problems. Duplicating the code is a bad idea. You should be using CreateProcess to create processes. Fundamentally I would say that your main issue is that copying code without understanding it is a bad idea. You then become unable to maintain it and are unable to critique it. 1 Share this post Link to post
Ian Branch 127 Posted March 13, 2019 Hi David, Thanks for your input. Appreciated. I have scrapped the existing routine(s) and gone a different route. All working as desired now. Regards, Ian Share this post Link to post