Which is the proper way to terminate a delphi application?

你说的曾经没有我的故事 提交于 2019-12-03 05:48:05

Application.Terminate() breaks the message loops in TApplication.Run() and TForm.ShowModal(), allowing the main thread to exit normally, perform necessary cleanups, etc.

Vcl.Forms.TApplication.Terminate

Ends application execution.

Call Terminate to end the application programmatically. By calling Terminate rather than freeing the application object, you allow the application to shut down in an orderly fashion.

Terminate calls the Windows API PostQuitMessage function to perform an orderly shutdown of the application. Terminate is not immediate. Terminate is called automatically on a WM_QUIT message and when the main form closes.

Halt(), on the other hand, is an immediate abnormal termination. Basically, ripping the process out of memory. Use it only in extreme situations where no other option is available.

System.Halt

Initiates the abnormal termination of a program.

Halt performs an abnormal termination of a program and returns to the operating system.

To perform a normal termination of a Delphi application, call the Terminate method on the global Application object. If the application does not use a unit that provides an Application object, call the Exit procedure from the main Program block.

I would like to terminate a Delphi application without executing any other code.

Neither Application.Terminate nor Halt will achieve that. The former performs an orderly termination. Lots of code will execute. Calling Halt is more hopeful. That is an abnormal termination. But unit finalization code is executed.

If you wish to exit as quickly as possible, executing the minimum amount of code along the way, call ExitProcess. That's the final step of Halt and by calling ExitProcess directly you avoid all the steps that Halt takes before it calls ExitProcess.

Just to leave a point on a extra problem if code must be on main form OnCreate.

Try such code on the Main Form OnCreate event. It does not work as expected, main form is shown, then the application is finished.

To be able to see it, add another form and put on its creation a long loop.

It seems like all the Application.CreateForm on the main project source are executed.

Sample code:

procedure TMyMainForm.FormCreate(Sender: TObject);
begin
     ShowMessage('[1] This must allways be shown');
     if mrOK=MessageDlg('Exit?',mtConfirmation,[mbOK,mbCancel],0)
     then begin
               Application.Terminate;
               Exit;
          end;
     ShowMessage('[2] This must not allways be shown');
end;
procedure TMyOtherForm.FormCreate(Sender: TObject);
begin
     ShowMessage('[3] This must not allways be shown');
end;

With that code messages [1] and [3] are allways shown.

Only way to not show [3] is to call Halt.

Note: Why such code on MainForm OnCreate? Simple answer could be, the exe checks conditions to be run and see they are not meet (missing files, etc), rude one (sorry for that), just because i want/need to.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!