Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the proper way to terminate a delphi application?

I would like to terminate a Delphi application without executing any other code line and I'm wondering about which is the proper way to do this. Furthermore, I would like to know if there's something wrong in what I'm actually doing at the moment. Basically, my code looks like this:

//Freeing all objects (Obj1.Free, etc..)
Application.Terminate;
Halt;

Is this the right way to stop a Delphi application or should it be done in another way?

like image 520
Hwau Avatar asked May 23 '15 17:05

Hwau


3 Answers

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.

like image 78
Remy Lebeau Avatar answered Sep 27 '22 22:09

Remy Lebeau


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.

like image 41
David Heffernan Avatar answered Sep 27 '22 23:09

David Heffernan


I had some problems with Application.Terminate, because I had to start the Form Close procedure, so I did only:

Form1.Close;

I found a new solution inside .dproj

begin
  ReportMemoryLeaksOnShutdown := True;
  Application.Initialize;
  Application.CreateForm(TFormMain, FormMain);
  if Not(VerifyCode()) then
  begin
      ShowMessage('Software unregistered!');
      Application.Terminate;
  end
  else
  Application.Run;
end.
like image 38
Wellington Telles Cunha Avatar answered Sep 27 '22 23:09

Wellington Telles Cunha