DataModule 在主窗体之前创建

发布于 2024-08-18 01:14:49 字数 1431 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光无声 2024-08-25 01:14:49

明显的原因是主窗体是否需要数据模块进行设置。例如,如果主窗体在其 OnCreate 中引用了其中的某些内容,那么当然必须首先准备好数据模块。

否则,这并不重要。

The obvious reason would be if the main form needs the Data Module for its setup. For example, if there's something in there that the main form references in its OnCreate, then of course the data module would have to be ready first.

Otherwise, it doesn't really matter.

万人眼中万个我 2024-08-25 01:14:49

我同意梅森的回答,因为它解释了为什么人们会这样做。但是,我认为这是一个不好的方法,因为它隐藏了 IDE 维护的代码中的依赖关系。在我看来,数据模块应该从自动创建列表中删除,并且应该在主窗体的 OnCreate 方法中创建。

I agree with Mason's answer because it explains why people may do this. However, I believe this is a bad approach because it hides the dependency in code that is maintained by the IDE. In my opinion, the data module should be removed from the auto create list and it should be created in the main form's OnCreate method.

思念绕指尖 2024-08-25 01:14:49

在这个问题上确实有两个阵营,而且都是正确的。

第一个让应用程序管理每个表单/数据模块的生命周期。在这种情况下,如果主窗体使用了数据模块,那么必须先创建它才能使用。这对于小型应用程序来说效果很好,但是当您访问具有多个表单的大型应用程序时,会产生加载开销......但是,一旦加载应用程序,则显示表单几乎是即时的,因为它已经在内存中创建了。由于每个表单/资源都已创建,因此在运行应用程序时也会占用大量内存。当您向应用程序添加新的表单/数据模块时,Delphi 也会“引导”您这种默认方法。如果您不在主窗体的 OnCreate 中使用数据模块,那么它在创建顺序中的位置可能较低,因为直到 Application.Run 启动后才会调用它。

第二个阵营想要处理每个表单/数据模块本身的创建和销毁(通常适用于除 MainForm 之外的所有表单)。此方法的优点是应用程序加载速度更快,并且在启动时立即消耗更少的内存。通常在这种情况下,主表单(或其他表单)完全管理它们使用的每个表单/数据模块的生命周期。此方法最适合具有多种形式的大型应用程序。

There really are two camps on this one, and both are correct.

The first lets the application manage the life of each form/data module. In this scenario, if the main form uses the data module, then it must be created before it can be used. This works fine for small applications, but there is a loading overhead when you get to larger applications with multiple forms...however once the application is loaded then displaying a form is almost instant since its already created in memory. Because each form/resource is already created there also is a large memory hit upon running the application. This method is the default one that Delphi "leads" you too as you add new forms/data modules to the application. If you don't use the datamodule in the OnCreate of the mainform, then it can be lower in the create order as it won't be invoked until after the Application.Run is launched.

The second camp wants to handle the creation AND destruction of each form/data module itself (generally for all forms other than the MainForm). The advantage to this method is that the application will load faster, and consume less memory immediately upon startup. Generally in this scenario, it is the main form (or other forms) which completely manage the life-cycle of each form/data module they use. This method works best for larger applications with many forms.

年少掌心 2024-08-25 01:14:49

只是因为这是确保 DataModule 内容可用于 MainForm 的最懒的方法。
如果您只有一个 DataModule,那么没有问题。

Just because it's the laziest way to assure the DataModule content are available to MainForm.
If you have just one DataModule, there's no trouble.

夜夜流光相皎洁 2024-08-25 01:14:49

在创建所有其他组件之前,不会显示 MainForm。所以基本上在这两种情况下你都会等待,天气数据模块是否首先创建。如果您的网格位于主窗体上,那么在尝试使用数据模块表(或查询)事件(打开后)中的类似内容时,您可能会遇到麻烦:

cxGrid1DBTableView1.Controller.TopRowIndex :=0;
cxGrid1DBTableView1.DataController.FocusedRowIndex := 0;

原因很明显:网格尚未创建。我必须承认,我也首先创建了数据模块。但也有一些原因,就像我刚才描述的那样,这样做是不切实际的。

MainForm is not displayed before all other components are created. So basically you will wait in both cases, weather data-module is created first or not. If your grid is on the main form then you might get in trouble when trying to use something like this from the data-modules table (or query's) event (after-open):

cxGrid1DBTableView1.Controller.TopRowIndex :=0;
cxGrid1DBTableView1.DataController.FocusedRowIndex := 0;

Reason is obvious: The grid isn't created yet .. I must admit, I also create datamodule first. But there are reasons,like the one I just described, when it's not practical to do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文