Delphi in initialization and finalization

Source: Internet
Author: User

OK, let's get to the point, first explain the location of initialization and finalization in the code unit:

************************************************************

12345678910111213141516171819202122232425262728293031323334353637383940414243 unitUnit1; interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs;typeTForm1 = class(TForm)    Button1: TButton;    procedureFormCreate(Sender: TObject);    procedureFormDestroy(Sender: TObject);private    { Private declarations }public    { Public declarations }end;varForm1: TForm1;implementationusesUnit2;{$R*.dfm}procedureTForm1.FormCreate(Sender: TObject);beginShowMessage(‘1 FormCreate‘);end;procedureTForm1.FormDestroy(Sender: TObject);beginShowMessage(‘1 Destroy‘);end;initializationShowMessage(‘1 ini‘);{单元初始化代码}finalizationShowMessage(‘1 final‘);{单元退出时的代码}end.

************************************************************

initialization--is placed in the cell before the end of the file, containing the code used to initialize the unit, which runs before the main program runs and runs only once.

The finalization--is placed in the unit in initialization and end. Contains the code when the unit exits. Runs when the program exits and runs only once.

onclose--is called when the Close button is clicked or when the event is executed.

oncreate--is called when the form is created. (My feeling: The event is executed after the form is created, that is, after the constructor is executed)

ondestroy--is called when the form is destroyed, and is typically used to destroy objects, variables, pointers, and so on, which are defined by the programmer themselves (defined, created, not dragged directly from the control Board). (Relationships with destructors I haven't figured out yet)

The following is the order of execution (if there are no initialization, OnCreate, OnClose, OnDestroy, finalization in the program, then the corresponding skip can be):

Program starts--executes initialization code---Executes the form's constructor and creates objects in the appropriate forms and cells (such as dragged control objects, global variables, and so on)--Executes the OnCreate event--The program runs-- Close the main form (this is where you call close of the main form or click the Close button of the main form)--Execute the OnClose event--Execute the OnDestroy event--and execute the code at finalization

The above is for a single form, the following is for multiple forms:

Create a new project that creates 3 forms, namely Form1, Form2, Form3, and Form1 the main form by default.

<1 begin>

The program starts and executes the code at initialization (first Form1 and then Form2 last FORM3)-executes the form's constructor and creates the corresponding form and the object in the cell (such as a dragged control object, global variable, etc.). The Create event (Form1 then Form2 last form3)-The program runs-closes the main form (here refers to the call to close of the main form or click the Close button of the main form)-executes the OnClose event of the main form-- Execute the OnDestroy event (Form3 then Form2 the last Form1, note yo, the order is upside down. )--Execute the code at finalization (first form3 then form2 the last Form1, notice Yo, the order is upside down. )

<1 end>

Why is OnDestroy and OnCreate in the opposite order? Why is finalization and initialization in the opposite order? Let's think about it!

Also: If I uses the Unit2 (Form2 code unit) in Form1, the order of execution is as follows:

<2 begin>

The program starts and executes the code at initialization (first Form2 and then Form1 last Form3)-executes the form's constructor and creates the corresponding form and the object in the cell (such as a dragged control object, global variable, etc.). The Create event (Form1 then Form2 last form3)-The program runs-closes the main form (here refers to the call to close of the main form or click the Close button of the main form)-executes the OnClose event of the main form-- Perform the OnDestroy event (Form3 then form1 the last Form2, note that it is not the same. )--Execute the code at finalization (first form3 then form1 the last Form2, note that it's not the same. )

<2 end>

Multiple forms (units) Something of some detail:

1) Initialization

The order of execution of initialization depends on which form is called first, and for <1>, because there is no uses and uses relationship between the 3 forms, they are executed in the order of the Project unit uses (in the engineering unit, as in the following):

************************************************************

Program Project1;

Uses

Forms,

Unit1 in ' Unit1.pas ' {Form1},

Unit2 in ' Unit2.pas ' {Form2},

Unit3 in ' Unit3.pas ' {FORM3};

{$R *.res}

Begin

Application.initialize;

Application.createform (TForm1, Form1);

Application.createform (TForm2, Form2);

Application.createform (TFORM3, FORM3);

Application.Run;

End.

************************************************************

If you change

Uses

Forms,

Unit1 in ' Unit1.pas ' {Form1},

Unit2 in ' Unit2.pas ' {Form2},

Unit3 in ' Unit3.pas ' {FORM3};

Become

Uses

Forms,

Unit2 in ' Unit2.pas ' {Form2},

Unit3 in ' Unit3.pas ' {Form3},

Unit1 in ' Unit1.pas ' {Form1};

Then is (first Form2 after form3 last Form1)

For <2>, because Unit2 is uses in Form1, Unit1 must be compiled before compiling Unit2. Order Nature is (first Form2 then Form1 finally Form3)

So initialization's execution order first looks at the uses and the uses relationship between the units (simply call the relationship) and then the uses order of the project files. (Oh, don't get mixed up by uses!) )

2) OnCreate

The order of execution of the OnCreate is determined by similar code in the engineering unit

************************************************************

Application.createform (TForm1, Form1);

Application.createform (TForm2, Form2);

Application.createform (TFORM3, FORM3);

************************************************************

Changes will occur accordingly.

3) OnDestroy

Contrary to the order of OnCreate. (In contrast, the order of creation is the opposite of the order of destruction, in order to avoid errors, which also reflects the rigorous thinking of programming requirements.) )

4) Finalization

Contrary to the order of initialization.

Besides saying OnClose,

This event is called when the form's close is called or when the Close button is clicked.

After the main form is close, the program exits.

The non-main form, close, is just the close form. (Don't say you're calling off other forms in onclose, those don't talk about the scope)

The OnClose event is not called when the program is forced to quit directly with Application.terminate. But OnDestroy and finalization are going to do it.

Said so much, do not know if there is no explanation white question. I hope you can understand (my ability to express poor, hehe ~ ~ ~).

Transferred from: http://my.oschina.net/kavensu/blog/191440

Delphi in initialization and finalization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.