Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-01-08
10
Medium Priority
?
1,611 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

722 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