tags:

views:

387

answers:

5

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?

+1  A: 

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.

Gamecat
The only downfall of this is that the application will flash on the screen briefly. To avoid the flash, first move the form off screen.
skamradt
@skamradt, good point. Although I would like to give an error message to let the user know why the application wasn't started.
Gamecat
A: 

I wrote a small application to test a theory and here is what I would suggest.

Call the CLOSE method.

The following example unit closes the application with no errors in D2009.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
begin
   close;
end;

end.
Ryan J. Mills
I can't call the close method because Eurekalog when picking up the exception prevents the main form from being closed at that point and the main form still comes up.
yozey
Well you can then either tell Eurekalog to ignore that exception (Type) or place the launch of the other program in the Project file. By placing it in the project file you can catch it before the main form is either created, or before the application.run command.
Ryan J. Mills
+10  A: 

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.

Rob Kennedy
I agree. Why not just try to launch the external program from dpr, and if that fails just not proceed. That way no need for Halt, wmClose or what have you.
Mihaela
Yes I think I will check the condition before the main form is created.
yozey
A nice and valid question and a nice and elegant answer +1 for both of you: the Pusher and the Popper :) Never overflow the stack! ;P
Peter Perháč
+2  A: 

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.

Loren Pechtel
A: 

While I fully agree with Rob Kennedy here, I want to note, that you may use EurekaLog's routines to control error dialog behaviour. For example:

uses
  ExceptionLog, ECore;
...
begin
  ForceApplicationTermination(tbTerminate);
  // ... <- Bad code goes there
end;

That way, the application will be closed right after displaying error dialog.

Alexander