Note: This article mainly discusses the execution sequence at the startup and exit of the Delphi Program. Some knowledge comes from the help of Delphi, some are from Delphi7 Programming Tutorial (this book only tells me the two keywords initialization and finalization, but I didn't understand it) (alas, the current book only discusses frame frames and does not discuss details, you can understand what you write. When someone asks or thinks about it, nothing can be understood! Not detailed! Alas, let's figure it out !!). The code is marked with two lines.
Now, go to the question. First, describe the position of initialization and finalization in the code unit:
**************************************** ********************
Unit unit1;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs;
Type
Tform1 = Class (tform)
Button1: tbutton;
Procedure formcreate (Sender: tobject );
Procedure formdestroy (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;
VaR
Form1: tform1;
Implementation
Uses unit2;
{$ R *. DFM}
Procedure tform1.formcreate (Sender: tobject );
Begin
Showmessage ('1 formcreate ');
End;
Procedure tform1.formdestroy (Sender: tobject );
Begin
Showmessage ('1 destroy ');
End;
Initialization
Showmessage ('1 ini '); {unit initialization code}
Finalization
Showmessage ('1 final'); {code when the unit exits}
End.
**************************************** ********************
Initialization-- Put the code in the unit before the end of the file and include the code used to initialize the unit. It runs only once before the main program runs.
Finalization-- Place the value between initialization and end. In the Unit, including the code when the unit exits. Run only once when the program exits.
Onclose-- Called when you click the close button or execute the event.
Oncreate-- Called when the form is created. (My feeling: this event is executed after the form is created, that is, after the constructor is executed)
Ondestroy-- Called when a form is destroyed, it is generally used to destroy objects, variables, and pointers defined by the programmer (defined and created by the programmer, rather than the controls dragged from the control board. (I haven't figured out the relationship with the Destructor yet)
The execution sequence is as follows (if the program does not contain any item in initialization, oncreate, onclose, ondestroy, and finalization, skip it accordingly ):
Program startup --> execute the code at initialization --> execute the constructor of the form and create objects in the corresponding form and unit (such as dragging control objects and global variables) --> execute the oncreate event --> program running --> close the main form (this refers to calling the close of the main form or clicking the close button of the main form) --> execute onclose event --> execute ondestroy event --> execute code at finalization
For a single form, the following is for multiple forms:
Create a project and create three forms: form1, form2, and form3. By default, form1 is the main form.
<1 begin>
Start the program --> execute the code at initialization (form1 first and form2 finally form3) --> execute the form constructor and create objects in the corresponding form and unit (such as dragging control objects, global variables, etc.) --> execute the oncreate event (first form1 and then form2 and finally form3) --> run the program --> close the main form (this refers to calling the close of the main form or clicking the close button of the main form) --> execute the onclose event of the main form --> execute the ondestroy event (first form3 and then form2 and finally form1. Note that the order is reversed .) --> Execute the code at finalization (first form3 and then form2 and finally form1. Note that the order is reversed .)
<1 end>
Why is the order of ondestroy different from that of oncreate? Why is the reverse order of finalization and initialization? Think about it!
In addition, if I uses unit2 (the code unit of form2) in form1, the execution sequence is as follows:
<2 begin>
Start the program --> execute the code at initialization (form2 first and form1 last form3) --> execute the form constructor and create objects in the corresponding form and unit (such as dragging control objects, global variables, etc.) --> execute the oncreate event (first form1 and then form2 and finally form3) --> run the program --> close the main form (this refers to calling the close of the main form or clicking the close button of the main form) --> execute the onclose event of the main form --> execute the ondestroy event (first form3 and then form1 and finally form2. Note that it is different .) --> Execute the code at finalization (first form3 and then form1 and finally form2. Note that it is different .)
<2 end>
Multiple Forms (units:
1) initialization
The execution order of initialization depends on which form is called first. For <1>, because there is no uses or uses relationship between the three forms, therefore, it is executed in the order of engineering unit uses (in the engineering unit, as follows ):
**************************************** ********************
Program project1;
Uses
Forms,
Unit1 in 'unit1. pa' {form1 },
Unit2 in 'unit2. pa' {form2 },
Unit3 in 'unit3. pa' {form3 };
{$ R *. Res}
Begin
Application. initialize;
Application. createform (tform1, form1 );
Application. createform (tform2, form2 );
Application. createform (tform3, form3 );
Application. Run;
End.
**************************************** ********************
If changed
Uses
Forms,
Unit1 in 'unit1. pa' {form1 },
Unit2 in 'unit2. pa' {form2 },
Unit3 in 'unit3. pa' {form3 };
Become
Uses
Forms,
Unit2 in 'unit2. pa' {form2 },
Unit3 in 'unit3. pa' {form3 },
Unit1 in 'unit1. pa' {form1 };
That is (form2 first, then m3 last form1)
For <2>, because uses in form1 has unit2, unit2 must be compiled before unit1. The order is (first form2, then form1, and finally form3)
SoThe execution sequence of initialization first checks whether there is a uses relationship between each other's units and the uses relationship (in short, the call relationship), and then looks at the order of the uses in the project file.(Don't confuse it with uses !)
2) oncreate
The execution sequence of oncreate is determined by the following code in the project unit:
**************************************** ********************
Application. createform (tform1, form1 );
Application. createform (tform2, form2 );
Application. createform (tform3, form3 );
**************************************** ********************
After the change, it changes accordingly.
3) ondestroy
The order is the opposite to that of oncreate.(On the contrary, the creation order is the opposite of the destruction order, so as to avoid errors, which also reflects the rigorous thinking of programming .)
4) finalization
Unlike initialization.
Let's talk about onclose,
This event is called when the close or close button of the form is called.
After the main form is closed, the program exits.
Non-main form close, only close this form.(Do not call the close of other forms in onclose, and those that do not discuss the scope)
When you use application. Terminate to force exit the program, the onclose event is not called. However, ondestroy and finalization must be executed.
After talking about this, I don't know whether the problem is explained. I hope you can understand (poor expression skills, haha ~~~).