How to terminate your Delphi application before it starts¶
It sounds like a contradiction: Why would you start you application and terminate it right away? Well, sometimes we can only "really" launch our main form if certain requirements are met.
- Does my configuration file exist?
- Can I connect to the database?
- Do other data files exist?
- Do my image resources exist?
- Is the hardware sufficient?
About the video¶
In all of these cases you might want to display an error message but not launch your application as a consequence. With Delphi VCL applications, it may seem simple at first. However, there is some housekeeping required in order to stop execution correctly.
The latest episode of "How it Works With Holger" gives you all the essential info you need.
In order to check requirements before launching your application, proceed as follows:
Create a new unit with
TAppController. The application controller will launch the application or terminate if necessary.
In this example, the
TDataControllerclass uses a SQLite database that needs to exist. Otherwise, the application cannot be run. Thus, the method
CanStartAppchecks if the file exists. It will be called by
TAppControllerbefore the main window of the application is shown.
The key of this concept is that the code from the DPR of your default VCL Windows Form application is moved into the
Runmethod of the application controller.
It is checked if
True. Otherwise, a message is shown. Note that
Application.Runis not called.
The source code in the DPR project file can be reduced to: