Delphi: Application Initialization - Best Practices / Approach

I regularly come across this and I'm just looking for the best practice / approach. I have an application containing a database / datamodule, and I want to start the database / datasets at startup without enabling "active at runtime" during development (the time in the database changes). Also, when starting the application, run the usual check "update check".

Given TForm event sequences and the results of various trial and error, I am currently using this approach:

I use the "Globals" entry set in the main form to store all global vars, has one element from the name called Globals.AppInitialized (boolean) and sets it to False in the "Initialization" section of the main form.

In the main form of the OnShow event (all forms are created by then), I am testing Globals.AppInitialized; if it is false, I start my “Initialization” material and then complete the “Global Variables .AppInitialized: = True” setting.

This seems to work very well, but is it the best? Looking for an understanding of other people's experience, ideas and opinions. TIA ..

+3
source share
7 answers

Usually I always turn off auto-creation of all EXCEPT forms for the main form, and possibly the primary datamodule.

, , , datamodule , . , , onCreate datamodule .

, , (-, , " " ), oncreate, ( ) , . :

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);

, , , , , "". , .

+10

, ( .dpr) Application.Run. ( .)

:

...
Application.CreateForm(TMainForm, MainForm);    
...
MainForm.ApplicationLoaded; // loads options, etc..
Application.Run;
...
+7

, , , .. IDE.

, Application , . , datamodule , datamodules , . datamodule StartUp ShutDown, "" Application.Run dpr. ShutDown .

, " " .

+3

, " " Delphi. , , , .

AppInitialized Initialization . ( datamodule), , UI ( One-Ring, ..)

:

  • .
  • "check for update" - . Firefox.
+2

, , ? Delphi . , .

, , , .

+1

, , , , db :

Application.CreateForm(TDmMain, DmMain);

  if DmMain.isDBConnected then
    begin
      Application.CreateForm(TDmVisualUtils, DmVisualUtils);
      Application.CreateForm(TfrmMain, frmMain);
    end;

  Application.Run;
+1

One trick I use is to put the TTimer in the main form, set the time to something like 300 ms, and do any initialization (db login, network file copies, etc.). Launching the application immediately opens the main form and allows you to perform any initialization operation. Users do not start multiple instances, thinking "Oh, I did not dbl-click ... I will do it again .."

0
source

All Articles