How To Porperly Close A Form
How To Porperly Close A Form
How To Porperly Close A Form
- Stack Overflow
I would like to terminate a Delphi application without executing any other code line and I'm
wondering about which is the proper way to do this. Furthermore, I would like to know if
25 there's something wrong in what I'm actually doing at the moment. Basically, my code looks
like this:
Is this the right way to stop a Delphi application or should it be done in another way?
2 Closing the main form is the proper way to go. If you like you can go straigt forward with
Application.MainForm.Close; – Sir Rufo May 23, 2015 at 17:24
1 Closing the MainForm simply calls Application.Terminate , so you can call that directly when needed.
However, do not that it can only be called in the context of the main thread, not a worker thread.
– Remy Lebeau May 23, 2015 at 17:26
To prevent additional code in the current procedure from executing, use Exit; after
Application.Terminate; instead of Halt; – crefird May 25, 2015 at 11:58
@SilverWarior - yes, the Close will first call DoClose() then Terminate. – Server Overflow Mar 29, 2019 at
11:51
Sorted by:
4 Answers
Highest score (default)
https://stackoverflow.com/questions/30415407/which-is-the-proper-way-to-terminate-a-delphi-application 1/4
31/10/22, 21:49 Which is the proper way to terminate a delphi application? - Stack Overflow
Halt() , on the other hand, is an immediate abnormal termination. Basically, ripping the
process out of memory. Use it only in extreme situations where no other option is available.
System.Halt
In this specific case, my goal is that to stop application without executing any other code line (I just
want to free everything and terminate the application). As Application.Terminate is not immediate,
should I use Halt or are there any possible trouble in using this? – Hwau May 24, 2015 at 8:23
2 Halt does not free very much, but it does try to do some internal system cleanup. If your goal is to
just bail out of the process immediately, you can use Halt() , but if that is still too much code to
execute, then you will have to resort to the Win32 API TerminateProcess() function. – Remy Lebeau
May 24, 2015 at 8:31
Thanks for clarification, I suppose Halt is enough for my requirements. Your messages have been really
helpful, +1 and accepted. – Hwau May 24, 2015 at 8:41
I would like to terminate a Delphi application without executing any other code.
12
Neither Application.Terminate nor Halt will achieve that. The former performs an orderly
termination. Lots of code will execute. Calling Halt is more hopeful. That is an abnormal
termination. But unit finalization code is executed.
https://stackoverflow.com/questions/30415407/which-is-the-proper-way-to-terminate-a-delphi-application 2/4
31/10/22, 21:49 Which is the proper way to terminate a delphi application? - Stack Overflow
If you wish to exit as quickly as possible, executing the minimum amount of code along the
way, call ExitProcess . That's the final step of Halt and by calling ExitProcess directly you
avoid all the steps that Halt takes before it calls ExitProcess .
4 ExitProcess will execute code. Such as DllMain. One should use TerminateProcess. – Alex May 24, 2015
at 22:22
@Alex TerminateProcess is indeed even more brutal. – David Heffernan May 25, 2015 at 5:09
TerminateProcess was once necessary evil for me. A third party library we needed was causing an
exception at exit. TerminateProcess = Exception gone. :-) – nurettin Feb 27, 2017 at 13:38
If you do not know the state of all threads in your process, it is better to call TerminateProcess than
ExitProcess – Ian Boyd Oct 24, 2017 at 14:24
I had some problems with Application.Terminate, because I had to start the Form Close
procedure, so I did only:
2
Form1.Close;
begin
ReportMemoryLeaksOnShutdown := True;
Application.Initialize;
Application.CreateForm(TFormMain, FormMain);
if Not(VerifyCode()) then
begin
ShowMessage('Software unregistered!');
Application.Terminate;
end
else
Application.Run;
end.
Just to leave a point on a extra problem if code must be on main form OnCreate.
1 Try such code on the Main Form OnCreate event. It does not work as expected, main form is
shown, then the application is finished.
To be able to see it, add another form and put on its creation a long loop.
https://stackoverflow.com/questions/30415407/which-is-the-proper-way-to-terminate-a-delphi-application 3/4
31/10/22, 21:49 Which is the proper way to terminate a delphi application? - Stack Overflow
It seems like all the Application.CreateForm on the main project source are executed.
Sample code:
With that code messages [1] and [3] are allways shown.
Note: Why such code on MainForm OnCreate? Simple answer could be, the exe checks
conditions to be run and see they are not meet (missing files, etc), rude one (sorry for that),
just because i want/need to.
I mean, when on main form creating You call Application.Terminate, it almost do nothing, because main
form creating are processed before Application.Run call from main application source in project file
".dpr". Terminate are breaks main loop, but it still not started! And Application.Run before start main
message handling loop goes to show previously created MainForm. But there are solition: it have
special flag to do not show form. Make Application.ShowMainForm := False; and main form will not
shown at all. – Nashev Sep 15, 2016 at 15:05
But [3] You still will see, it part of form's constructor. Exit from constructor does not break object's
construction. It can be breaked by raising an exception/ You can call Abort, for example, but some thing
will be must handle it. – Nashev Sep 15, 2016 at 15:06
https://stackoverflow.com/questions/30415407/which-is-the-proper-way-to-terminate-a-delphi-application 4/4