Solved

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

Posted on 2013-01-08
10
1,371 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
  • 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 22

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
 
LVL 25

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 22

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now