Delphi:应用初始化 - 最佳做法/方法 [英] Delphi: App initialization - best practices / approach
问题描述
给定TForm事件序列和各种试用和错误的结果,我目前正在使用这种方法:
我使用在主窗体中设置的Globals记录来存储所有的全局变量,有一个名为Globals.AppInitialized(boolean)的元素,并在主窗体的初始化部分设置为False。
在主窗体的OnShow事件(所有表单都创建的时候),我测试Globals.AppInitialized;如果它是假的,我运行我的初始化的东西,然后完成通过设置Globals.AppInitialized:= True。
这似乎工作得很好,但它是最好的方法寻找别人的经验,观点和观点。 TIA ..
我通常总是关闭主窗体和主数据模块的所有形式的自动创建EXCEPT。 / p>
我学到了一个窍门,可以将您的数据模型添加到您的项目中,允许它在您的主窗体之前自动创建和创建。然后,当您的主窗体创建时,数据模块的onCreate将已经运行。
如果您的应用程序有一些代码可以设置控件的焦点(创建时无法做的事情,因为它的不可见)然后创建一个用户消息并将其发布到您的oncreate中的表单。一旦处理表单消息循环,就会处理消息SHOULD(不保证)。例如:
const
wm_AppStarted = wm_User + 101;
type
Form1 = class(tForm)
:
procedure wmAppStarted(var Msg:tMessage);消息wm_AppStarted
结束
//在您的oncreate事件中添加以下内容,这将导致您的wmAppStarted事件触发。
PostMessage(handle,wm_AppStarted,0,0);
我不能想到这个消息从未被处理过的一次,而是调用是将其添加到消息队列中,如果队列已满,那么它将被删除。只要知道边缘案例存在。
I run into this regularly, and am just looking for best practice/approach. I have a database / datamodule-containing app, and want to fire up the database/datasets on startup w/o having "active at runtime" set to true at design time (database location varies). Also run a web "check for updates" routine when the app starts up.
Given TForm event sequences, and results from various trial and error, I'm currently using this approach:
I use a "Globals" record set up in the main form to store all global vars, have one element of that called Globals.AppInitialized (boolean), and set it to False in the Initialization section of the main form.
At the main form's OnShow event (all forms are created by then), I test Globals.AppInitialized; if it's false, I run my "Initialization" stuff, and then finish by setting Globals.AppInitialized := True.
This seems to work pretty well, but is it the best approach? Looking for insight from others' experience, ideas and opinions. TIA..
I generally always turn off auto creation of all forms EXCEPT for the main form and possibly the primary datamodule.
One trick that I learned you can do, is add your datamodule to your project, allow it to auto-create and create BEFORE your main form. Then, when your main form is created, the onCreate for the datamodule will have already been run.
If your application has some code to say, set the focus of a control (something you can't do on creation, since its "not visible yet") then create a user message and post it to the form in your oncreate. The message SHOULD (no guarantee) be processed as soon as the forms message loop is processed. For example:
const
wm_AppStarted = wm_User + 101;
type
Form1 = class(tForm)
:
procedure wmAppStarted(var Msg:tMessage); message wm_AppStarted;
end;
// in your oncreate event add the following, which should result in your wmAppStarted event firing.
PostMessage(handle,wm_AppStarted,0,0);
I can't think of a single time that this message was never processed, but the nature of the call is that it is added to the message queue, and if the queue is full then it is "dropped". Just be aware that edge case exists.
这篇关于Delphi:应用初始化 - 最佳做法/方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!