Ian Branch 128 Posted January 10, 2023 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
Mark- 29 Posted January 10, 2023 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
David Heffernan 2354 Posted January 10, 2023 I'd terminate the process 2 Share this post Link to post
Uwe Raabe 2064 Posted January 10, 2023 This code should terminate the application after the form creation without making the main form visible: Application.ShowMainForm := False; Application.Terminate; 2 Share this post Link to post
programmerdelphi2k 237 Posted January 10, 2023 (edited) 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 January 10, 2023 by programmerdelphi2k Share this post Link to post
Tom Chamberlain 47 Posted January 10, 2023 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
Remy Lebeau 1436 Posted January 10, 2023 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
Remy Lebeau 1436 Posted January 10, 2023 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
Tom Chamberlain 47 Posted January 10, 2023 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 Share this post Link to post
Fr0sT.Brutal 900 Posted January 11, 2023 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
Lars Fosdal 1793 Posted January 11, 2023 That is interesting. Although, at that point the creation has been completed. Share this post Link to post
chkaufmann 17 Posted January 12, 2023 I use ExitProcess(AExitCode). Christian Share this post Link to post