在Delphi中更改单元的初始化顺序 [英] changing the initialization order of the unit in Delphi

查看:176
本文介绍了在Delphi中更改单元的初始化顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Delphi XE7开发Windows 32位应用程序。

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.

是否可以设置优先级?
我试图在dpr文件中写入初始化部分,但是编译器拒绝了。

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.

推荐答案

简单来说,初始化部分的执行顺序是在任何<$中引入单元的顺序。 c $ c>使用子句。但是,由于仅在对该单元本身引用的任何单元进行初始化之后(尚未初始化)才执行该单元的初始化,因此它要复杂得多。

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).

ie给出:

program Foo;

  uses
    Unit1,
    Unit2,
    Unit3;







unit Unit1;

interface

  uses
    Unit3;

然后,单元初始化顺序为:

Then the unit initialization order will be:

Unit3
Unit1
Unit2

这是因为 Unit1 引入了 Unit3 ,所以即使 Unit1 dpr 使用中首先列出, Unit3 实际上首先被初始化,然后是 Unit1的初始化

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.

因此,唯一可以绝对确定任何条件的唯一方法在 any 之前被初始化的一个单元是首先在 DPR uses 子句中列出该单元不依赖于任何其他单元(除非那些单元不依赖于或以其他方式干扰正在执行的初始化)。

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) .

它不一定必须<当然是严格。例如如果您使用的是替换内存管理器(例如FastMM),则绝对需要成为 dpr uses 子句中列出的第一单元。您只需要确保需要初始化的单位在列出其他(单位)之前先列出,这可能会使其他单位进入:

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;

当然,如果您要首先初始化的单元需要要先于其他 进行初始化,包括RTL单元(以与FastMM等相同的方式),则需要在 dpr 通过更早地声明您的单位来使用列表。

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.

这篇关于在Delphi中更改单元的初始化顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆