Delphi:应用程序初始化 - 最佳实践/方法 [英] Delphi: App initialization - best practices / approach

查看:30
本文介绍了Delphi:应用程序初始化 - 最佳实践/方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常遇到这种情况,只是在寻找最佳实践/方法.我有一个包含数据库/数据模块的应用程序,并希望在启动时启动数据库/数据集,而无需在设计时将运行时活动"设置为 true(数据库位置各不相同).还要在应用程序启动时运行网络检查更新"例程.

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.

鉴于 TForm 事件序列以及各种反复试验的结果,我目前正在使用这种方法:

Given TForm event sequences, and results from various trial and error, I'm currently using this approach:

我使用在主表单中设置的Globals"记录来存储所有全局变量,有一个名为 Globals.AppInitialized (boolean) 的元素,并在主表单的 Initialization 部分将其设置为 False.

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.

在主窗体的 OnShow 事件中(所有窗体到那时创建),我测试 Globals.AppInitialized;如果它是假的,我运行我的初始化"的东西,然后通过设置 Globals.AppInitialized := True 来完成.

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.

这似乎工作得很好,但这是最好的方法吗?从他人的经验、想法和意见中寻找洞察力.TIA..

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.

我了解到您可以做的一个技巧是将您的数据模块添加到您的项目中,让它在您的主表单之前自动创建和创建.然后,当您的主窗体创建时,数据模块的 onCreate 将已经运行.

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.

如果你的应用程序有一些代码要说,设置一个控件的焦点(你不能在创建时做这件事,因为它还不可见")然后创建一个用户消息并将它发布到你的 oncreate 中的表单.一旦表单消息循环被处理,消息应该(不保证)被处理.例如:

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屋!

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