Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: code after Application.Run does not execute if user reboots/shuts down

Tags:

winapi

delphi

I have some code placed after "Application.Run;" which is normally run when the app shuts down. This point is never reached however when the user reboots or shuts Windows down.

Can this be solved without capturing WM_ENDSESSION? (and not involving any form, I'd like the code to remain after application.run at the end of the dpr)

like image 261
hikari Avatar asked Aug 29 '14 13:08

hikari


1 Answers

VCL already listens for WM_ENDSESSION (the hidden application window), and terminates the application when the session is ending. You can add a terminate procedure in the .dpr (or elsewhere) which will be called among possible other terminate procedures:

program Project1;

uses
  Vcl.Forms,
  sysutils,
  Unit2 in 'Unit2.pas' {Form2};

{$R *.res}

function OnTerminate: Boolean;
begin
  Result := True;
  // do some short work
end;

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm2, Form2);
  AddTerminateProc(OnTerminate);
  Application.Run;
end.
like image 112
Sertac Akyuz Avatar answered Nov 14 '22 02:11

Sertac Akyuz