Jump to content
Ian Branch

Close application during form create??

Recommended Posts

Hi Team,

What is the cleanest/safest way to close an Application during Main Form Creation?

I tried just Close; but it keeps going. 😞

 

Regards & TIA,

Ian

Share this post


Link to post

Post a close message to the main form.

 

procedure PostCloseMessage(aHand:hwnd);inline;
begin
 PostMessage(aHand,WM_CLOSE,0,0);
end;

 

PostCloseMessage(self.handle);

Share this post


Link to post

This code should terminate the application after the form creation without making the main form visible:

Application.ShowMainForm := False;
Application.Terminate;

 

  • Like 2

Share this post


Link to post

as said Melander, and adding...

 

PostQuitMessage( 0 ) it will close the app in any place on project!

 

uses
  Unit2;

procedure TForm1.FormCreate(Sender: TObject);
begin
  PostQuitMessage(0); // quit
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2 := TForm2.Create(self);
end;

// ----- Form2 ----
{$R *.dfm}

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree; // to avoid "memory leaks" when using "TForm2.Create(self);", else, try another approach!
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  PostQuitMessage(0); // close the app same in this place!
end;

 

Edited by programmerdelphi2k

Share this post


Link to post

You could change the DPR file like so:

begin
  Application.Initialize;
  Application.ShowMainForm := False;
  Application.CreateForm(TfrmMainForm, frmMainForm);
  if frmMainForm.DoWeShowTheForm then
   Application.ShowMainForm := True
  else
   Application.Terminate;
  Application.Run;
end.

Then in the normal form OnCreate set a flag and check it in the DPR, this never shows the form if you want to terminate the program, the forms OnDestroy runs so you can do any cleanup and no memory leaks.

Share this post


Link to post
1 hour ago, programmerdelphi2k said:

PostQuitMessage( 0 ) it will close the app in any place on project!

Application.Terminate() calls PostQuitMessage() internally:

// ----- Form1 ----

uses
  Unit2;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2 := TForm2.Create(self);
end;

// ----- Form2 ----

procedure TForm2.FormCreate(Sender: TObject);
begin
  Application.Terminate;
end;

No need for using OnClose/caFree when Form1 is the Owner of Form2, since Application.Terminate()/PostQuitMessage() is asynchronous, so the Form1 and Form2 objects will still be created, Form1 will take ownership of Form2, and then the Application object will destroy the Form1 object, and thus the Form2 object, when the Application object is destroyed after the quit message makes Application.Run() exit.

Share this post


Link to post
1 hour ago, Tom Chamberlain said:

You could change the DPR file like so:


begin
  Application.Initialize;
  Application.ShowMainForm := False;
  Application.CreateForm(TfrmMainForm, frmMainForm);
  if frmMainForm.DoWeShowTheForm then
   Application.ShowMainForm := True
  else
   Application.Terminate;
  Application.Run;
end.

In which case, I wouldn't even bother with calling Application.Terminate(), just skip calling Application.Run() instead:

begin
  Application.Initialize;
  Application.ShowMainForm := False;
  Application.CreateForm(TfrmMainForm, frmMainForm);
  if frmMainForm.DoWeShowTheForm then
  begin
    Application.ShowMainForm := True;
    Application.Run;
  end;
end.

 

Share this post


Link to post
1 hour ago, Remy Lebeau said:

In which case, I wouldn't even bother with calling Application.Terminate(), just skip calling Application.Run() instead:


begin
  Application.Initialize;
  Application.ShowMainForm := False;
  Application.CreateForm(TfrmMainForm, frmMainForm);
  if frmMainForm.DoWeShowTheForm then
  begin
    Application.ShowMainForm := True;
    Application.Run;
  end;
end.

 

I thought there was a problem with skipping the Application.Run, that is why the call to Application.Terminate is in there.  It puts the terminate message on the que and the Application.Run processes it correctly and you get a clean exit.  My 'DoWeShowTheForm' actually is a splash screen with login prompt from a dynamically loaded DLL so maybe that is why I did it with the Application.Terminate, it has been many years ago since that was written.  I did try it without the terminate and skipping the run and it does work, FastMM did not report any issues, but I still put the terminate back in :classic_smile:

Share this post


Link to post
17 hours ago, Lars Fosdal said:

I'd probably just


Halt;

 

 

Funny, just yesterday I fought with bugs in the same area. I used Halt as well but it failed to terminate the app when called from main form's OnShow

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×