Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
As you already know by your searches, Application.Terminate lets any resource and memory to be freed before to call the Windows postquitmessage (that is invoked by Application.Terminate). That's why is called a "gracefully" method.
Halt just quits immediately the program.
What we can guess early is that in a formless application, where the unit Forms is not used, we can't call Application.Terminate, as we don't have a Tapplication Object. In this case we should use Halt
Anyway, in your case you should use the ExitCode variable to assign a preferred value to it
Just try it in a new form, using ErrorAddr procedure to get back an Error Dialog displaying the exit code and the Error Address, with this simple piece of code
My suggestion is to write error proof code. Exiting from application is not an option if you get an error. Use try ... exception blocks and try to detect possible problems depending on wrong values.
I would love to be able to write error proof code, but this program is just one in a chain so I must be able to capture unexpected errors, log them, and then quit the application. If you have a suggested methodology on how to fix all possible errors on the fly, please share your wisdom. I would love to know the secret. :D
I tried using Application.Terminate (and Form1.Close), but found that the application continues to run even after it hits these lines. Is it possible that I cannot use either of these in the FormCreate? Here's a snippet of code:
procedure TForm1.FormCreate(Sender: TObject);begin if Not FileExists(ParamStr(1)) then begin System.ExitCode := 1; Form1.Close; //DOES NOT SHUT DOWN!?! Application.Terminate; //DOES NOT SHUT DOWN?!? Halt(1); //DOES SHUT DOWN! end;
There's lots of other code in the actual program, but I can't see how it makes a difference in behavior. So why would the first two requests to close the application fail and only the "Halt" succeed?
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
You're calling Application.Terminate during OnCreate, that means that you can't close the Form 'til it's completely created, as Terminate let's execute the code before to close the application.
Application.Teminate calls PostQuitMessage, which sends a WM_QUIT message to the applications message queue. This message is handled normally and closes your app.
If you want to prevent your app from executing commands after Application.Terminate until the message is handled, then simple place an EXIT command behind Application.Terminate.
Form.Close does not have an effect in FormCreate event, because the form itself does not exist at that time...
Application.Terminate or Halt() ?