Reputation:
When I generate new application project on Delphi, the first Line of Implementation in source Project1.pas is the following.
Application.Initialize;
Application.CreateForm(Tform1, form1);
Application.Run;
Then in event OnCreate of Form1 I realize all initialization
I need, like
Open FdConnections, Activate Queries and so on.
I am also closing all Queries and Connection in OnClose of form. Would be Initialization
and Finalization
one good alternative to do do that? What's this commands purpose, and what different of OnCreate?
var Form1:TForm1;
Implementation
Initialization
Begin
FdConnection1.Open;
FdQuery1.open;
FdQuery2.Open;
...
End;
Finalization
Begin
FdQuery1.Close
...
FdConnection.Close;
End; {Could It works?}
Upvotes: 1
Views: 7894
Reputation: 30735
To follow the order of events when a Delphi application starts up, try the following:
Create an Initialization
section in your Form1's unit, put some inconsequential code
in it and set a debug breakpoint on it. The point of this code is simply so that you can
put a breakpoint on it.
Create a Finalization
section in your Form1's unit, put some code in that and put
a breakpoint on it.
Put a break point on Application.Initialize in your .Dpr file.
Compile and run the app.
The debugger will stop on the breakpoint you created in step 1. If you view the call stack,
you will see that the code is being called as a result of being called from the RTL routine
InitUnits
which calls in turn the initialization section of each unit compiled into your
.Exe. The exact order they are called is determined by the compiler at compile time
and presumably determined by a depth-first traversal of the USEd units.
Next, the debugger will stop on the Application.Initialize BP in the .Dpr file. By the time
execution reaches there, the Initialization
sections of all the units compiled into the
.Exe will have been called.
When you close the application, the debugger will stop on the Finalization
BP you set
in step 2. If you trace out of it, you will find that it calls the Finalization
sections
of the units in the reverse order to the order the Initialization
sections were called. And
these Initialization
and Finalization
sections are called only once per execution of
the .Exe.
It ought to be apparent from the above that the calls to the Initialization/Finalization
sections are unrelated to whether any classes in the units are ever instantiated. The prime
reason to instantiate a class in an Initialization
section is to ensure that the class
instance exists before Application.Initialize is called (or the equivalent of Main()
is
called in a console application).
Upvotes: 4
Reputation: 54
The Initialization and Finalization sections execute exactly once, no matter how many instances of a Form are created. Contrast that with the OnCreate, OnClose, and OnDestroy events of a Form, which execute once for each instance.
In your example, the Form in question is only created once, so the difference really applies more to Forms that are (or can be) created (instantiated) multiple times, whether each instance is closed or not.
Upvotes: 2