Delphi中initialization和finalization

注:本文主要是 讨论delphi程序启动时和退出时的执行顺序 ,期间有些知识来源于Delphi帮助,有些来自《Delphi7程序设计教程》(这本书只告诉我有initialization 和 finalization 这两个关键字,但是没讲明白)(唉,现在的书只讨论框框架架,不讨论细节,写出来的东西让你看能看懂,别人一问或者自己一想,就是什么都不懂!讲的不细致呀!唉,自己琢磨吧!!)。代码用两排‘*’标明。


好了,进入正题,首先说明initialization 和 finalization 在代码单元中的位置:
************************************************************
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');{单元初始化代码}
finalization
  ShowMessage('1 final');{单元退出时的代码}

end.
************************************************************
initialization —— 在单元中放在文件结尾前,包含用来初始化单元的代码,它在主程序运行前运行并且只运行一次。
finalization —— 在单元中放在 initialization 和 end. 之间,包含了单元退出时的代码。在程序退出时运行并且只运行一次。
OnClose —— 在点击关闭按钮或执行该事件时调用。
OnCreate —— 在窗体创建时调用。(我的感觉:该事件是在窗体创建之后,即执行构造函数后才执行的)
OnDestroy —— 在窗体销毁时调用,一般用来销毁程序员自己定义(自己定义、创建,而不是直接从控件板拖拉过来的控件)的对象、变量、指针等等。(与析构函数的关系我还没弄清)

下面是执行顺序(如果程序中没有 initialization、 OnCreate、 OnClose、 OnDestroy、 finalization 中的任何一项,则相应跳过即可):
程序启动 --> 执行 initialization 处的代码 --> 执行窗体的构造函数并创建相应窗体和单元中的对象(如拖拉的控件对象,全局变量等) --> 执行 OnCreate 事件 --> 程序运行 --> 关闭主窗体(此处指调用主窗体的Close或点击主窗体的关闭按钮) --> 执行 OnClose 事件 --> 执行 OnDestroy 事件 --> 执行 finalization 处的代码

以上是对于单个窗体而言,下面是对于多个窗体:
新建一工程,创建3个Form,分别是form1、form2、form3,默认情况下form1为主窗体。
<1 begin>
程序启动 --> 执行 initialization 处的代码(先 form1 然后 form2 最后 form3 ) --> 执行窗体的构造函数并创建相应窗体和单元中的对象(如拖拉的控件对象,全局变量等) --> 执行 OnCreate 事件(先 form1 然后 form2 最后 form3 ) --> 程序运行 --> 关闭主窗体(此处指调用主窗体的Close或点击主窗体的关闭按钮) --> 执行主窗体的 OnClose 事件 --> 执行 OnDestroy 事件(先 form3 然后 form2 最后 form1 ,注意哟,顺序倒过来了。) --> 执行 finalization 处的代码(先 form3 然后 form2 最后 form1 ,注意哟,顺序倒过来了。)
<1 end>
为什么 OnDestroy 和 OnCreate 的顺序相反?为什么  finalization和 initialization 的顺序相反?大家好好想想吧!


另外:如果我在form1中uses 一下Unit2(form2的代码单元)执行顺序如下:
<2 begin>

程序启动 --> 执行 initialization 处的代码(先 form2 然后 form1 最后 form3 ) --> 执行窗体的构造函数并创建相应窗体和单元中的对象(如拖拉的控件对象,全局变量等) --> 执行 OnCreate 事件(先 form1 然后 form2 最后 form3 ) --> 程序运行 --> 关闭主窗体(此处指调用主窗体的Close或点击主窗体的关闭按钮) --> 执行主窗体的 OnClose 事件 --> 执行 OnDestroy 事件(先 form3 然后 form1 最后 form2 ,注意注意,不一样了。) --> 执行 finalization 处的代码(先 form3 然后 form1 最后 form2 ,注意注意,不一样了。)
<2 end>


多个窗体(单元)一些细节性的东西:
1)initialization
initialization 的执行顺序取决于哪个窗体先被调用,对于<1>,由于3个窗体之间没有 uses 和被 uses 的关系,所以按照工程单元uses 中的顺序执行(在工程单元中,如下):
************************************************************
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.
************************************************************
如果改变
uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2},
  Unit3 in 'Unit3.pas' {Form3};

成为
uses
  Forms,
  Unit2 in 'Unit2.pas' {Form2},
  Unit3 in 'Unit3.pas' {Form3},
  Unit1 in 'Unit1.pas' {Form1};
那么就是(先form2后form3最后form1)
对于<2>,由于form1中uses了Unit2,那么在编译Unit1之前必须先编译Unit2。顺序自然是(先 form2 然后 form1 最后 form3)
所以 initialization 的执行顺序首先看彼此单元之间是否有uses和被uses关系(简单的说就是:调用关系), 然后再看工程文件的uses的顺序。(呵呵,不要被uses弄混了!)

2)OnCreate
OnCreate 的执行顺序由工程单元中的如下类似代码决定
************************************************************
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.CreateForm(TForm3, Form3);
************************************************************
改变之后则相应发生改变。

3)OnDestroy
与OnCreate的顺序相反。(相反时一种对应,创建的顺序与销毁的顺序是相反的,这样是为了避免发生错误,这样也体现了编程要求思维严谨。)

4)finalization
与 initialization 的顺序相反。

再说说OnClose,
当调用窗体的Close或点击关闭按钮时调用该事件。
主窗体Close后,程序就退出。
非主窗体Close,仅仅是Close本窗体。
(别说你在OnClose中调用其他窗体的关闭,那些不再讨论范围)

直接用Application.Terminate强制退出程序时,不调用OnClose事件。但 OnDestroy 和 finalization  要执行。

说了这么多,不知道有没有说明白问题。希望大家能领悟(俺表达能力比较差,呵呵~~~)。

 
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Delphi Logger { 超简单实用的DELPHI日志单元 1.0.0 优化版 引用本单元即可使用 一共四个方法 procedure log4error(msg: AnsiString); //写ERROR级别的日志 procedure log4info(msg: AnsiString); //写INFO级别的日志 procedure log4debug(msg: AnsiString); //写DEBUG级别的日志 function log4filename():AnsiString; //得到当前日志文件全名 一个配置文件 log4me.ini #配置文件和主程序在同一目录.没有这个文件或不在主目录则不写日志 [log4me] #path,日志的存放目录.必须是主程序目录及子目录. #例子:主程序目录 #path=. #例子:子目录 #path=temp\logs path=logs #level,日志等级,只能是 error,info,debug之一 #为error时,只有log4error打印的日志被输出. #为info时,log4error和log4info打印的日志被输出. #为debug时,log4error,log4info,log4debug打印的日志都被输出. level=info 一个可选工具 tail.exe 命令行输入 >tail.exe -1000f 日志文件名 即可动态查看日志输出 或用程序调用 var cmd :AnsiString; log_file:AnsiString; begin log_file := log4filename(); //得到当前日志文件全名 cmd := ExtractFilePath(ParamStr(0)) + 'tail.exe -1000f "'+ log_file +'"'; WinExec(PAnsiChar(cmd),SW_SHOWNORMAL); //程序调用 tail.exe工具来查看日志 } unit log4me; interface uses classes, sysutils, ComObj, windows,IniFiles; procedure log4error(msg: AnsiString); //写ERROR级别的日志 procedure log4info(msg: AnsiString); //写INFO级别的日志 procedure log4debug(msg: AnsiString); //写DEBUG级别的日志 function log4filename():AnsiString; //得到当前日志文件全名 var log_filename: AnsiString; //日志文件全名 implementation var log_ThreadLock: TRTLCriticalSection; // 临界区 log_fileStream: TFileStream; log_filepath: AnsiString; //日志文件路径 log_initime:TDateTime; log_doerror, log_dodebug, log_doinfo: Boolean; procedure log_init(); var tmpStr:String; begin log_doerror := False; log_dodebug := False; log_doinfo := False; if FileExists(ExtractFilePath(ParamStr(0)) + 'log4me.ini') then begin with TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'log4me.ini') do try log_filepath := ExtractFilePath(ParamStr(0)) + '\' + ReadString('log4me','path','logs') + '\'; tmpStr := LowerCase(ReadString('log4me','level','info')); log_doerror := (tmpStr = 'debug') or (tmpStr = 'info') or (tmpStr = 'error'); log_doinfo := (tmpStr = 'debug') or (tmpStr = 'info'); log_dodebug := (tmpStr = 'debug') ; finally Free; end; end; log_initime := Now; end; procedure log4me_addLog(filename: AnsiString; p: PAnsiChar); var fmode :Word; tmp:AnsiString; begin //进入临界区,多线程时可以保护资源 EnterCriticalSection(log_ThreadLock); try try //如果要写的日志文件和打开的不同(在程序第一次运行和跨天的时候出现) //则关闭打开的日志文件。 if filename log_filename then begin log_filename := filename; if Assigned(log_fileStream) then begin log_fileStream.Free; log_fileStream := nil; end; end; //如果要写的日志文件没有打开(在程序第一次运行和跨天的时候出现) //则打开日志文件。 if not Assigned(log_fileStream) then begin if FileExists(log_filename) then fmode := fmOpenWrite or fmShareDenyNone else fmode := fmCreate or fmShareDenyNone ; log_fileStream := TFileStream.Create(log_filename,fmode); log_fileStream.Position := log_fileStream.Size; end; //在日志文件写入日志 log_fileStream.Write(p^, strlen(p)); except on E:Exception do begin try tmp := ExtractFilePath(ParamStr(0)) + 'log4me_err.log'; if FileExists(tmp) then fmode := fmOpenWrite or fmShareDenyNone else fmode := fmCreate or fmShareDenyNone ; with TFileStream.Create(tmp, fmode) do begin Position := Size; tmp := FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', Now) + ' ' + E.Message + #13#10; Write(tmp[1],Length(tmp)); Free; end; except end; end; end; finally //无论如何,离开临界区 LeaveCriticalSection(log_ThreadLock); end; end; procedure log4write(msg: AnsiString); var strline: AnsiString; begin // 最多每秒重加载一次配置文件 if (Now() - log_initime) > (1/(24*60*60)) then log_init(); //日志开头加时间 strline := FormatDateTime('hh:nn:ss.zzz', Now) + ' ' + msg + #13#10; //写到当天的日志文件 log4me_addLog(log_filepath + FormatDateTime('yyyy-mm-dd', Now) + '.log', PAnsiChar(strline)); end; //-----下面4个是对外方法------------------------- function log4filename():AnsiString; begin Result := log_filename; end; procedure log4error(msg: AnsiString); begin if log_doerror then log4write('[error]' + msg); end; procedure log4info(msg: AnsiString); begin if log_doinfo then log4write('[info ]' + msg); end; procedure log4debug(msg: AnsiString); begin if log_dodebug then log4write('[debug]' + msg); end; // ----------- 类初始化 -------------// initialization InitializeCriticalSection(log_ThreadLock); log_init; log4info('log4me:application starting....'); // ----------- 类销毁 -------------// finalization log4info('log4me:application stoping....'); DeleteCriticalSection(log_ThreadLock); if Assigned(log_fileStream) then log_fileStream.Free; end.
需要先安装Patch1。 Patch 2 for RAD Studio 10.4 now available This patch addresses a number of issues in RAD Studio 10.4, pertaining to Delphi Compiler, the RAD Studio IDE in general and the new LSP-based Code Insight in particular, plus C++ Builder Android exceptions and some debugger issues. The installation of this patch requires a prior installation of Patch #1 (separately available on GetIt and in the download portal). Installing this patch is recommended for all RAD Studio 10.4 customers. Note that this patch is fairly large to download (around 190 MB). The patch includes detailed installation instructions as part of the Readme. Please read the steps carefully (or the corresponding steps in this blog post), as the GetIt download does not install the patch automatically. You must follow the instructions in order to install. Just using GetIt is not enough. List of Customer Reported Issues Addressed in 10.4 Patch 2 RAD Studio 10.4 Patch #2 addresses the following issues reported by customer on Embarcadero Quality Portal (https://quality.embarcadero.com): RSP-29628 VCL Grids bug RSP-29560 [REGRESSION] Misalignment in TStringGrid, StretchDraw method in OnDrawCell RSP-29412 Compiler generates incorrect code for if-then RSP-29402 Delphi 10.4 TStringGrid.OnDrawCell bug RSP-29374 Wrong rect coords in TStringGridDrawCell, so image are drawn at wrong position RSP-29347 [DelphiLSP] IDE Crashes when view form as text is selected and running LSP server RSP-29310 Internal error L891 when linking because of complex types based on records with class var RSP-29299 CODEGEN bug in managed fields initialization, associated with new management operators. RSP-29271 [DelphiLSP] Code Insight adds unneeded () when changing procedures/functions RSP-29256 Compiler generates wrong code for template function RSP-29227 Incorrect property value obtained from the record RSP-29226 Access violation with working code under 10.2 RSP-29218 compiling static library under Android error E4620 processing resource .fmx -2 raised RSP-29172 Access Violation when opening License Manager RSP-29142 GoTo statements not working RSP-29136 Dialog constantly pops up during debugging RSP-29129 iOS App simply crashes with a TWebBrowser on it. RSP-29127 Compiler internal error if you ignore the result of a function that returns a generic record RSP-29124 ICE E1812 RSP-28989 License Manager has access violation error when i click on Workstation Licenses RSP-28887 Space does not finish code completion RSP-28857 Default(T) generates bad code for managed record RSP-28821 [Regression] TStringGrid.OnDrawCell parameter Rect contains wrong values RSP-28808 Project options dialog page "Delphi Compiler" is not populated when opening the dialog RSP-28796 RVO for M-records: initialisation of local variables RSP-28761 [REGRESSION] E2154 Type 'T' needs finalization - not allowed in variant record RSP-28737 Compiler error when inlining new Bit Counting Standard Functions RSP-28735 Managed Records Causing Internal Compiler Error RSP-28717 Delphi Package fails to compile RSP-28701 Bind visually on TDBGRID kills the IDE RSP-28669 [BadCG] Value M-record parameters: improper AddRefRecord RSP-28659 RVO for M-records: assignment to local variables RSP-28616 [BadCG] Operator Assign should not allow non-default calling conventions RSP-28615 [BadCG] In the absence of Initialize, finalisation is not guaranteed for local variables RSP-28552 Poor code generation for local managed record variables RSP-28499 Options - Translation tools - Font - Corrupted? RSP-28476 LSP ErrorInsight in Structure Pane only shows one keystroke after editor RSP-28400 [BadCG] Operator Assign is not always invoked for fields RSP-28372 [Regression] Bad codegen in function returning generic type RSP-27268 C++ Builder 10.3.3 Android Exceptions RSP-27251 Internal error when trying to inline with optimization on RSP-24079 Package version is broken RSP-23403 Build for linux 64 error RSP-23024 Record helper class constructor gives senseless compiler warning RSP-22318 Pointer type check missed when object field is a dynarray RSP-21554 Compiler generates incorrect code for parameterized record type RSP-21248 Const dynamic array unexpectedly contains uninitialized data RSP-20372 A generic "reference to function" will only match the first of several overloaded functions RSP-19714 Win32 compiler - Memory corruption with array helpers RSP-18241 *.c source files, added to C++ project, got added to DeploymentManager file list RSP-18148 AV in TList.Remove (64-bit compiler only)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值