changing the initialization order of the unit in Delphi

我怕爱的太早我们不能终老 提交于 2019-12-12 10:39:19

问题


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.


回答1:


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.




回答2:


The initialization sections are executed in the order that the units appear in the uses clause.

So, you may force your unit to initialize first by moving it up in the uses list.



来源:https://stackoverflow.com/questions/52103407/changing-the-initialization-order-of-the-unit-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!