Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application.Terminate doesn't

Tags:

delphi

If I call Application.Terminate OR Application.MaiForm.Close inside a method. the application doesn't terminate!

procedure doSomething;
var
  ErrorFound: boolean;
begin
  [...]
  try
    if ErrorFound then
      Application.Terminate;
  finally
  [...]
  end;
end;

I cannot understand why.

Please note that I'm calling Application.Terminate from inside a Try...Finally block, in the Try section.

like image 458
Fabio Vitale Avatar asked Feb 22 '23 11:02

Fabio Vitale


1 Answers

Most likely you are calling Application.Terminate from a busy loop that itself does not terminate. If you enter an event handler but never return from it then calling Application.Terminate or Application.MainForm.Close would indeed be futile. These routines that close an application are cooperative. They post messages to the main message loop requesting that the application gracefully shuts down.

For example, the following code would exhibit the symptoms you describe:

procedure TMyForm.Button1Click(Sender: TObject);
begin
  while True do
    Application.Terminate;
end;

Another (more likely) possibility is that you are calling this function from inside a modal form but not closing the modal form.

Unless you show more code, we are reduced to making guesses like this.

like image 117
David Heffernan Avatar answered Mar 06 '23 19:03

David Heffernan