Delphi: App initialization - best practices / approach

前端 未结 7 1002
北恋
北恋 2020-12-15 01:47

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 start

相关标签:
7条回答
  • 2020-12-15 02:27

    One trick I use is to place a TTimer on the main form, set the time to something like 300ms, and perform any initialization (db login, network file copies, etc). Starting the application brings up the main form immediately and allows any initialization 'stuff' to happen. Users don't startup multiple instances thinking "Oh..I didn't dbl-click...I'll do it again.."

    0 讨论(0)
  • 2020-12-15 02:28

    I use a primary Data Module to check if the DB connection is OK and if it doesn't, show a custom component form to setup the db connection and then loads the main form:

    Application.CreateForm(TDmMain, DmMain);
    
      if DmMain.isDBConnected then
        begin
          Application.CreateForm(TDmVisualUtils, DmVisualUtils);
          Application.CreateForm(TfrmMain, frmMain);
        end;
    
      Application.Run;
    
    0 讨论(0)
  • 2020-12-15 02:43

    You may want to directly interfere with the project source (.dpr file) after the form creation calls and before the Application.Run. (Or even earlier in case.)

    This is how I usually handle such initialization stuff:

    ...
    Application.CreateForm(TMainForm, MainForm);    
    ...
    MainForm.ApplicationLoaded; // loads options, etc..
    Application.Run;
    ...
    
    0 讨论(0)
  • 2020-12-15 02:46

    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.

    0 讨论(0)
  • 2020-12-15 02:46

    I don't know if this is helpful, but some of my applications don't have any form auto created, i.e. they have no mainform in the IDE.

    The first form created with the Application object as its owner will automatically become the mainform. Thus I only autocreate one datamodule as a loader and let this one decide which datamodules to create when and which forms to create in what order. This datamodule has a StartUp and ShutDown method, which are called as "brackets" around Application.Run in the dpr. The ShutDown method gives a little more control over the shutdown process.

    This can be useful when you have designed different "mainforms" for different use cases of your application or you can use some configuration files to select different mainforms.

    0 讨论(0)
  • 2020-12-15 02:48

    I'm not sure I understand why you need the global variables? Nowadays I write ALL my Delphi apps without a single global variable. Even when I did use them, I never had more than a couple per application.

    So maybe you need to first think why you actually need them.

    0 讨论(0)
提交回复
热议问题