当前位置: 动力学知识库 > 问答 > 编程问答 >

winapi - ExitProcess from the OnShow event of MainForm in 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?

网友答案:

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.

网友答案:

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.

网友答案:

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.

网友答案:

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.
网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: