I'm using Delphi XE7 for developing windows 32 bit application.
My application contains many units, which has an initialization section. I need to initialize one particular initialization section at first.
Is it possible to set the priority? I have tried to write the initialization section in dpr file, but the compiler has rejected this.
Please help me to execute the particular initialization section at first. Thanks in advance.
Delphi first runs the initialization section for all the units the Name unit uses, in order of appearance, starting with the interface units, followed by the implementation units. A unit’s initialization section runs after Delphi runs the initialization section for all used units.
Setting a new memory manager is a common example of a unit that must be initialized first. The order of unit initialization means you cannot know for certain when your unit will be initialized, but all the units used in the interface section will be initialized first.
This topic covers the overall structure of a Delphi application: the program header, unit declaration syntax, and the uses clause. Divide large programs into modules that can be edited separately. Create libraries that you can share among programs. Distribute libraries to other developers without making the source code available.
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.
In simple terms, initialization sections are executed in the order in which the units are introduced in any uses
clauses. But it is a little more complicated than that due to the fact that initialization of a unit is performed only after the initialization of any units that that unit itself references (where they have not already been initialized).
i.e. Given:
program Foo;
uses
Unit1,
Unit2,
Unit3;
unit Unit1;
interface
uses
Unit3;
Then the unit initialization order will be:
Unit3
Unit1
Unit2
This is because Unit1
introduces Unit3
, so even though Unit1
is listed first in the dpr uses, Unit3
is in fact initialized first, followed by the initialization of Unit1
.
If you remember that the initialization
section occurs after any uses
clauses in a unit, it does make sense.
Therefore the only way to be absolutely sure of any one unit being initialized before any other is to list it first in the DPR uses
clause and for that unit to take no dependencies on any other units (except where those units are not dependent on, or otherwise interfere with, the initialization being performed) .
It need not necessarily be strictly first of course. e.g. if you are using a replacement memory manager (such as FastMM) then this will absolutely need to be the very first unit listed in your dpr uses
clause. You simply need to make sure that the unit you need to be initialised before any other (of your units) is then listed before any other unit which might bring your other units in:
program Foo;
uses
FastMM, // MUST be first but won't bring any of 'my' units in, so this is OK
SysUtils, // These too are fine coming next because again they don't
Forms, // reference 'my' units
MyInitUnit, // <- This is where it is important to list 'my' guaranteed first/earliest
// initialisated unit of all 'my' units
MyFirstAppUnit, // And now the rest ...
etc;
Of course, if the unit that you wish to initialise first does need to be initialised before any other, including RTL units (in the same way that FastMM etc need to be) then you would need to reflect that in the dpr uses
list by declaring your unit earlier still.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With