Recent

Author Topic: OnEndSession(  (Read 5946 times)

dbannon

  • Hero Member
  • *****
  • Posts: 3667
    • tomboy-ng, a rewrite of the classic Tomboy
OnEndSession(
« on: May 01, 2019, 02:49:10 pm »
I wish to save some data if the system my app is running on is shutdown.

Events like OnClose are not fired and I see, in this forum, lots of references to Application.OnEndSession but it does not work for me ??

Code: Pascal  [Select][+][-]
  1.  procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.     Application.OnEndSession:=@OnEndSession;
  4. end;
  5.  
  6. procedure TForm1.OnEndSession(Sender: Tobject);
  7. var
  8.   f: textfile;
  9. begin
  10.     assignfile(f, '/home/dbannon/CloseLog.txt');
  11.     rewrite(f);
  12.     writeln(f,'shutting down.');
  13.     closefile(f);
  14. end;  
                   

The file, CloseLog.txt does not appear if the system is shutdown gracefully while the app is running. If I manually shut down the app thats fine, I see the log file but if the system shuts it down, nothing.

I'm using linux and most (?) of the people mentioning OnEndSession appeared to be using Windows. Is this functionality Windows only perhaps ? Or have I missed something .....

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Firewrath

  • New Member
  • *
  • Posts: 35
Re: OnEndSession(
« Reply #1 on: May 01, 2019, 05:32:15 pm »
Yeah, I had these issues too.
I got it working on Windows, and I have a post here of how I got it working with example programs: (OTT_OnClose_Logging.zip)
https://forum.lazarus.freepascal.org/index.php/topic,43210.msg302569.html#msg302569

It works for me when saving setting to an INI file on shutdown.
Hope it helps. ^-^
(I can't get Lazarus to install on this rather old Linux laptop I have, so I can't test it. Sorry. =/)
« Last Edit: May 01, 2019, 05:35:29 pm by Firewrath »
Sorry. I currently don't have Internet Access. So my replies might take a week. -_-

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: OnEndSession(
« Reply #2 on: May 01, 2019, 10:45:12 pm »
I tried for days to do almost exactly what you want: save the configuration if the system is shutdown.

It just doesn't work in Linux. On shutdown, apparently, Linux just terminates/kill the application without sending any signal to it.

Now, how other applications do it (because they do) I have not the least idea: I resorted to saving the config immediately each time it was changed.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thausand

  • Sr. Member
  • ****
  • Posts: 458
Re: OnEndSession(
« Reply #3 on: May 01, 2019, 11:33:26 pm »
It just doesn't work in Linux. On shutdown, apparently, Linux just terminates/kill the application without sending any signal to it.
I no sure how work (i no time look now). But maybe have look fpSigAction ? Maybe have set good mask ?
- https://www.freepascal.org/docs-html/rtl/baseunix/fpsigaction.html
- deprecate: https://www.freepascal.org/docs-html/rtl/baseunix/fpsignal.html

if program terminate then before can make AddExitProc https://www.freepascal.org/docs-html/rtl/system/addexitproc.html but maybe that no soon for Lazarus program

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: OnEndSession(
« Reply #4 on: May 01, 2019, 11:42:57 pm »
I no sure how work (i no time look now). But maybe have look fpSigAction ? Maybe have set good mask ?

Nope. I tried that too and it didn't work. I spent quite a funny weekend over this, trying this and that and those other things ;)

The simple fact is that apparently the system sends NO signal whatsoever to the application. From the application's point of view, it's happily running at one second and the next it's absolutely dead and out of the memory.

There must be some way of intercepting the shut-down (other apps do it) but I don't know what they are and could find nothing throughout that famous weekend. :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Firewrath

  • New Member
  • *
  • Posts: 35
Re: OnEndSession(
« Reply #5 on: May 02, 2019, 12:37:07 am »
Unfortunately I checked this when I was leaving for the day, but since I plan to move over to Linux in the future, this interested me and I did some googling, I have not had time to Fully read all of this, but some of it is interesting. Note this does not apply to Pascal.
But if someone can figure out how it's done, then maybe a similar thing can be done in Pascal...

https://stackoverflow.com/questions/2832376/how-to-detect-pending-system-shutdown-on-linux
https://askubuntu.com/questions/1010120/intercept-shutdown-call-and-run-script-to-allow-or-prevent-shutdown
https://unix.stackexchange.com/questions/122557/how-does-the-system-shutdown-of-a-linux-kernel-work-internally
 
Sorry. I currently don't have Internet Access. So my replies might take a week. -_-

dbannon

  • Hero Member
  • *****
  • Posts: 3667
    • tomboy-ng, a rewrite of the classic Tomboy
Re: OnEndSession(
« Reply #6 on: May 02, 2019, 12:43:48 am »
Wow, thats pretty serious isn't it ?

I only spent a day Lucamar, not as bad as you  :-)


Its not documented on the wiki (that I could find, I might fix that at least and have a read of Firewrath's notes.

Thanks for response folks !

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

john horst

  • Jr. Member
  • **
  • Posts: 68
    • JHorst
Re: OnEndSession(
« Reply #7 on: May 02, 2019, 01:27:13 am »
https://www.freepascal.org/docs-html/rtl/baseunix/fpsigaction.html BaseUnix does work. Open the .lpr and add the main code.

kill -HUP pidof is captured correctly as HANGUP when I tested it.

The ifdef for Linux is not needed in that example and depreciated according to the docs.

Code: Pascal  [Select][+][-]
  1. program sigtest;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Interfaces, // this includes the LCL widgetset
  10.   Forms, sig, BaseUnix
  11.   { you can add units after this };
  12.  
  13. {$R *.res}
  14.  
  15. Var
  16.    oa,na : PSigActionRec;
  17.  
  18. Procedure DoSig(sig : cint);cdecl;
  19.  
  20. begin
  21.    writeln('Receiving signal: ',sig);
  22. end;
  23.  
  24. begin
  25.   RequireDerivedFormResource:=True;
  26.   Application.Scaled:=True;
  27.   Application.Initialize;
  28.   Application.CreateForm(TForm1, Form1);
  29.   new(na);
  30.   new(oa);
  31.   na^.sa_Handler:=SigActionHandler(@DoSig);
  32.   fillchar(na^.Sa_Mask,sizeof(na^.sa_mask),#0);
  33.   na^.Sa_Flags:=0;
  34.   if fpSigAction(SigUsr1,na,oa)<>0 then
  35.   begin
  36.     writeln('Error: ',fpgeterrno,'.');
  37.     halt(1);
  38.     end;
  39.   Application.Run;
  40. end.                              

You could also use OnCloseQuery which is fired before the form is closed.
« Last Edit: May 02, 2019, 01:48:26 am by john horst »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: OnEndSession(
« Reply #8 on: May 02, 2019, 02:58:04 am »
Yes, fpsigaction works normally, but try this:

Code: Pascal  [Select][+][-]
  1. Procedure DoSig(sig : cint);cdecl;
  2. var f: TextFile;
  3. begin
  4.   Assign(f, 'test.txt');
  5.   Rewrite(f);
  6.   Writeln(f, 'Signal ', sig, ' at ', DateTimeToStr(Now));
  7.   Close(f);
  8. end;

Run the program and, while it's running, shutdown the system normally. The file will not be created, much less written too.

And no, OnCloseQuery and OnClose are not called either. That would have been too easy :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

john horst

  • Jr. Member
  • **
  • Posts: 68
    • JHorst
Re: OnEndSession(
« Reply #9 on: May 02, 2019, 03:52:31 am »
@lucamar I got you now. Yes, you will be fighting the DE if you are expecting a wait or  sigterm to be sent on the way it handles a shutdown.  KDE vs Gnome vs sudo shutdown. KDE i believe tries to wait and let the application close while another DE will exit so close your apps like it says before losing data and shutting down.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: OnEndSession(
« Reply #10 on: May 02, 2019, 07:23:24 am »
I tried a lot of options, believe me, even to polling system files, and none of them worked with any fiability.

As I said: a full, funny weekend of testing (and rebooting the computer I don't know how many times!) ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

dbannon

  • Hero Member
  • *****
  • Posts: 3667
    • tomboy-ng, a rewrite of the classic Tomboy
Re: OnEndSession(
« Reply #11 on: May 02, 2019, 03:12:55 pm »
Yeah, I rebooted my system more than a Windows Vista user would .....
Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

john horst

  • Jr. Member
  • **
  • Posts: 68
    • JHorst
Re: OnEndSession(
« Reply #12 on: May 02, 2019, 05:37:14 pm »
It's really only possible if you have control of the environment. It can be done though.

https://unix.stackexchange.com/questions/122590/graceful-shutdown-in-archlinux

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: OnEndSession(
« Reply #13 on: May 02, 2019, 07:12:42 pm »
The big problem with that and similar "solutions" is that, as you say, you must have full control of the environment ... and your applicaiton doesn't usually have it (and would be bad manners to change it if it had :) )

Those solutions are just "user solutions". Which doesn't help your program if the user is not "savvy" enough. ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: OnEndSession(
« Reply #14 on: May 02, 2019, 07:52:56 pm »
Hi!

I can confirm Lucamars experience with Linux: No way to get a signal from a shutdown. I checked it years ago with Suse 9.x and half a year ago again with Suse 13.1. Neither SIGTERM nor SIGKILL could be catched with FPSigaction  https://www.freepascal.org/docs-html/rtl/baseunix/fpsigaction.html . It works perfect, if you send a signal to your Test App, but not with shutdown.

There was a discussion bevor in this forum: https://forum.lazarus.freepascal.org/index.php?topic=13866.0

So what to do? If you change some options which are stored in an IniFile, write them to disk as soon as possible after the change.

If you handle with some data i did it this way:
Declare a global boolean:

Code: Pascal  [Select][+][-]
  1. var DataChanged: Boolean;
  2.  

Every time you change the the data, set it

Code: Pascal  [Select][+][-]
  1. DataChanged := true;
  2.  

Use a timer with 10 seconds tic

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Timer1Timer(Sender: TObject);
  2. begin
  3. if Datachanged then
  4.     begin
  5.     ..... save your data
  6.     Datachanged :=false;
  7.     end;
  8. end;

So you loose only 10 seconds of your work if the app crashes or the system goes down.

Winni

 

TinyPortal © 2005-2018