Recent

Author Topic: Exception if "Application.Terminate" is called in ...FormCreate  (Read 1429 times)

hs40470

  • Newbie
  • Posts: 1
If an application detects that it is not able to start because of "something" is missing (in my case a special app-specific font installed) and try to stop running the app by calling "Application.Terminate" in (any) ...FormCreate callback "TApplication.Run" will be still called and it execution will cause an exception.
I modified "TApplication.Run" by adding 3 lines, so it's now:

procedure TApplication.Run;
begin
  if not Terminated then // <-- THIS IS ADDED
  begin
    if (FMainForm <> nil) and FShowMainForm then
    begin
      WidgetSet.AppSetupMainForm(FMainForm);
      FMainForm.Show;
    end;
    WidgetSet.AppRun(@RunLoop);
  end;
end;

The additional line solves the problem without modifing any project specific code. Here is a snippet of my app source (sorry for the german message text):

procedure TMainForm.FormCreate(Sender: TObject);
var
    lStp: boolean;

begin
  lStp := Screen.Fonts.IndexOf(TM_FontName) < 0;
  try
    if lStp then
      ShowMsgDlg(CreateMessageDialog('Die benötigte Schriftart "' + TM_FontName +
                                     '" ist nicht'#13#10'installiert: ' +
                                     'Das Programm muss deshalb beendet werden',
                                     mtCustom, [mbOK]))
    else
    begin
...
...
...
    end;
  finally
    if lStp then
      Application.Terminate();
  end;
end;
« Last Edit: August 12, 2024, 09:38:35 pm by hs40470 »

ASerge

  • Hero Member
  • *****
  • Posts: 2314
Re: Exception if "Application.Terminate" is called in ...FormCreate
« Reply #1 on: August 12, 2024, 10:28:41 pm »
Simplified example:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   Dlg: TForm;
  4. begin
  5.   Dlg := CreateMessageDialog('Test', 'Test', mtCustom, [mbOk]);
  6.   try
  7.     Dlg.ShowModal;
  8.   finally
  9.     Dlg.Free;
  10.     Application.Terminate;
  11.   end;
  12. end;
Windows x64. Lazarus 3.4. No Error.
Maybe there is an error in the unknown ShowMsgDlg procedure?

Joanna

  • Hero Member
  • *****
  • Posts: 975
Re: Exception if "Application.Terminate" is called in ...FormCreate
« Reply #2 on: August 21, 2024, 09:16:23 am »
@ hs40470
Have you ever considered using code tags ? Or Do you prefer the spacious  copy paste look ?  :D
« Last Edit: August 21, 2024, 09:23:39 am by Joanna »
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

 

TinyPortal © 2005-2018