Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When do form.close and Application.terminate really close? 2

Status
Not open for further replies.

bearsite4

Programmer
Sep 29, 2001
79
AU
I've been using these 2 functions quite a bit and I'm not sure how they work. Intuition would have it that they close the form or the app exactly where they are called. However, I've noticed that sometimes the statements after it in the procedure are executed before the app terminates and even sometimes it executes the rest of the program.

With regards to form.close am I right in assuming that if the form that form.close acts on is the first one created by the application in the dpr file than the whole app will close and if not then only that form will close?
 
If you close the main form of the application, the application will terminate. If you have a child form and want to close the application, use application.terminate

Regards S. van Els
SAvanEls@cq-link.sr
 
Hi bearsite4,

What SvanEls said is only true is if only one form is is
the memory. If you use form close the application will leave
a bit of data in the memory like where it can find other
forms. And stuff like that. Evenso an app will only be
terminated if you use application terminate. This must be
true because I have seen (NOT made) formless apps. For
example (I dont know if you have ever visited this site) at
(this site does not exist anymore)
there was the source for a trojan horse up for download and
that app did not include any forms (this was back in the days that this kinda stuff actually interrested me ;-)). And it was written in Delphi 3.0 Standard. I didn't understand how it worked because I just started programming and I dont have the source anymore because my comp crashed a while ago after a mistake I made in one of my own programs. I will try to find that source code for you and post the URL here.

I hope this helps,

BobbaFet
Everyone has a right to my opinion.
E-mail me at cwcon@programmer.net
 
If you look at the source code for the TCustomForm class (parent of TForm) the Close function checks to see if Application.MainForm=self, and if it does, it calls Application.Terminate. So, closing the main form and calling application.Terminate do exactly the same thing. TealWren
 
Thanks for clarifying that point for me guys.

Can anyone help me with the first part of my question. This is particularly important as I have a password procedure and sometimes when I call application.terminate if the password's wrong the program does not terminate then and there but still executes some of my other code. Does anyone else have this problem or is it just me?

 
Hmmm... I would recommend putting your password check in a modal dialog that comes up before your main form is initialized, so that none of your program forms are even created unless they get past the password check.

We do this in our project source (dpr):

Application.Initialize;
fLogin := TfLogin.Create(Application);
if fLogin.ShowModal = mrOk then
Application.CreateForm(TfMain, fMain);
Application.Run;
TealWren
 
BobbaFet, if you have a formless application, it will run from top to bottom (like the msdos progs). Windows based programs are event driven and wait for "user" reaction.
Bearsite4 I don not understand this password question, generally you ask a password when you start a program, not when you are quiting.

Regards S. van Els
SAvanEls@cq-link.sr
 
I'll have to have a closer look at that when I'm outta Uni TealWren.

To Svanels, I do ask for the password when the program starts but i terminate the program *if* the user enters an incorrect one.
 
If you don't want to get into your project source have a look at the Halt procedure. It's nice since calling halt will stop your application's main form from being drawn on the screen before disappearing.

DjangMan
 
I read in a book (written by some members of the Delphi development team) that Halt was a bad procedure because it doesn't ensure freeing up of resources.
 
If you do not want to edit the .dpr, you can make a loginform which returns a boolean condition.
I use this procedure for log-on to a database. If the user makes an error the program doesn't terminate, so he can try again. An example is:

function GetLoginParams(ALoginParams: TStrings): Boolean;
var
LoginForm: TLoginFrm;
begin
Result := False;
LoginForm := TLoginFrm.Create(Application);
try
if LoginForm.ShowModal = mrOk then
begin
ALoginParams.Values['USER NAME'] := LoginForm.edtUsername.Text;
ALoginParams.Values['PASSWORD'] := LoginForm.edtPassWord.Text;
Result := True;
end;
finally
LoginForm.Free;
end;
end; S. van Els
SAvanEls@cq-link.sr
 
Thanks again everybody. But let me reiterate :) I've already got my password code working and everything, I just want to know why sometimes when I when I call form.close or application.terminate the program does not exit right away but (using the debugger to step) I notice that other statements in my code are executed.
 
There are lots of reasons why things might execute after you terminate the program. Anything that is freed will have destructor code that needs to run, as well as any finally blocks you might have.

Closing your form actually posts a windows message (CM_RELEASE, it looks like), so the rest of the things in the message queue will be handled before the form is closed as well.

TealWren
 
Thanks TealWren, that really explains it. So given the message nature, I guess if you really absolutely needed other code not to execute your only option would be to include an infinite loop after the terminate statement so it does nothing else until windows processes the message.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top