从其它平台迁移而来
AfterConstruction、BeforeDestruction是TObject本身就有的方法,Loaded是从TComponent才有的方法,好好利用的话就可以更精准的控制对象的生命周期或者初始化/清理工作。虽然知道这点,而且也经常在自己的程序中使用,但还是会经常搞错执行顺序,因此专门记录一下以备忘备查。
Form
graph TD
f1[inherited Create 前] -->
f2[inherited Loaded 前] -->
f3[inherited Loaded 后] -->
f4[inherited Create 后] -->
f5[inherited AfterConstruction 前] -->
f6[FormCreate] -->
f7[inherited AfterConstruction 后] -->
f8[FormResize] -->
f9[FormShow] -->
f10[FormCloseQuery] -->
f11[FormClose] -->
f12[inherited BeforeDestruction 前] -->
f13[FormHide] -->
f14[FormDestroy] -->
f15[inherited BeforeDestruction 后] -->
f16[inherited Destroy 前] -->
f17[inherited Destroy 后]
DataModule
graph TD
d1[inherited Create 前] -->
d2[inherited Loaded 前] -->
d3[inherited Loaded 后] -->
d4[inherited Create 后] -->
d5[inherited AfterConstruction 前] -->
d6[DataModuleCreate] -->
d7[inherited AfterConstruction 后] -->
d8[inherited BeforeDestruction 前] -->
d9[DataModuleDestroy] -->
d10[inherited BeforeDestruction 后] -->
d11[inherited Destroy 前] -->
d12[inherited Destroy 后]
Frame
graph TD
f1[inherited Create 前] -->
f2[inherited Loaded 前] -->
f3[inherited Loaded 后] -->
f4[inherited Create 后] -->
f5[inherited AfterConstruction 前] -->
f6[inherited AfterConstruction 后] -->
f7[FrameResize] -->
f8[inherited BeforeDestruction 前] -->
f9[inherited BeforeDestruction 后] -->
f10[inherited Destroy 前] -->
f11[inherited Destroy 后]
总结
-
Loaded是在Create的过程执行的,应该是用来做一些加载资源之类或其它辅助构造的工作 -
AfterConstruction是在构造之后调用OnCreate事件,若类无OnCreate事件则可以用AfterConstruction代替来完成一些初始化工作 -
BeforeDestruction是在析构之前调用OnDestroy事件,若类无OnDestroy事件则可以用BeforeDestruction代替来完成一些反初始化工作