Solved

Which is Better "Application.Terminate" or "Halt(1)"?

Posted on 2013-01-08
10
1,558 Views
Last Modified: 2013-01-09
I've heard conflicting advice on how to end an application when an error is encountered.

Ideally, I want to make sure I pass an error code back to a calling application, but also cleanup the current application to avoid memory leaks, etc.

Some have said using...

Halt(1);

Open in new window


...is sufficient, but I've heard elsewhere that Halt should be used sparingly since resources are not guaranteed to be freed.

I'm leaning towards using...

System.ExitCode := 1;
Application.Terminate;

Open in new window

...but I'm not positive that this passes the correct ExitCode.

Can anyone confirm for me the "best" choice. And if it's neither, what would you recommend? I'm using Delphi XE.

Thanks!
0
Comment
Question by:surfbored
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 38756850
Read this for differences between HALT and TERMINATE...
Application.Terminate or Halt() ?
0
 
LVL 19

Expert Comment

by:Thommy
ID: 38756861
And also have a look at this prior EE question...
ExitCode for my application
0
 
LVL 23

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 250 total points
ID: 38757069
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
procedure TForm1.Button1Click(Sender: TObject);
begin
  ExitCode := 10;
  ErrorAddr := Addr(Self);
  Application.Terminate;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ErrorAddr := Addr(Self);
  Halt(20);
end;

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 38758643
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.
0
 
LVL 1

Author Comment

by:surfbored
ID: 38759069
@sinisav

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
0
 
LVL 1

Author Comment

by:surfbored
ID: 38759102
@Thommy and Ferruccio68

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;

Open in new window

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?

Thanks for any thoughts.
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 38759218
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.
0
 
LVL 19

Accepted Solution

by:
Thommy earned 250 total points
ID: 38759313
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...
0
 
LVL 1

Author Comment

by:surfbored
ID: 38759408
Thanks Experts, as always, you've been a terrific help! I'm raising the Points so I can split them.
0
 
LVL 1

Author Closing Comment

by:surfbored
ID: 38759422
Each answer had excellent advice that I used virtually all of! Thanks again.
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month3 days, 15 hours left to enroll

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question