Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExitProcess from the OnShow event of MainForm in Delphi

Tags:

winapi

delphi

I have an application that on startup checks some conditions and launches an external program in the OnShow event of the Main Form. The problem is that if there is an error when launching the external program, I want the application to terminate immediately. But there is an issue with that, in that EurekaLog catches my exceptions and somehow disrupts the message loop there by negating all calls to Application.Teminate and any other normal shutdown methods.

So here is my question, would ExitProcess be the best route then to immediately terminating my application when this condition exists?

like image 593
yozey Avatar asked May 26 '09 19:05

yozey


3 Answers

By the time OnShow has fired, you're too far into the program to decide that you don't really want the program to run. You should make that determination sooner. OnShow is not the place to decide that the form shouldn't be shown.

This is the sort of thing you should check before you even create the main form. Put your checks in the DPR file, and if you determine that the program shouldn't run, then simply call exit.

begin
  Application.Initialize;
  if not ApplicationShouldReallyStart then
    exit;
  Application.CreateForm(TMainAppForm, MainAppForm);
  Application.Run;
end.

Fill in your own implementation of ApplicationShouldReallyStart. (And it really should be a separate function, not in-line in the DPR file. The IDE gets confused if the begin-end block in the DPR file gets too complex.)

Aside from that, do not call ExitProcess. Call Halt instead. Halt calls ExitProcess, but it also calls unit finalization sections and other Delphi-specific process-shutdown tasks.

like image 182
Rob Kennedy Avatar answered Nov 09 '22 03:11

Rob Kennedy


Work WITH the system, not AGAINST it! You can't simply die in the middle of things. If you want to die do it within the rules--WM_CLOSE or maybe your own routine that says why it's dying and then sends a WM_CLOSE.

like image 38
Loren Pechtel Avatar answered Nov 09 '22 04:11

Loren Pechtel


You better send a wmClose message to the window. Else you have a big chance to get into trouble because of other messages send to the form.

like image 28
Toon Krijthe Avatar answered Nov 09 '22 03:11

Toon Krijthe