Jump to content
Sign in to follow this  
Ian Branch

Issue closing external apps..

Recommended Posts

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 by Sherlock
Used correct code tags

Share this post


Link to post

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. 

  • Like 1

Share this post


Link to post

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×