Recent

Author Topic: gracefully close my app  (Read 2856 times)

Sann-X!

  • New Member
  • *
  • Posts: 21
gracefully close my app
« on: October 30, 2018, 11:50:39 am »
Hi,
I have GUI app on armbian. Desktop environment is xfce4. If I shutdown OS then my app is terminated immediately. Events (OnClose etc) and finalization sections aren't executed. Why?

Thaddy

  • Hero Member
  • *****
  • Posts: 9285
Re: gracefully close my app
« Reply #1 on: October 30, 2018, 12:12:18 pm »
OnCloseQuery is the event you are looking for. But even then a hard reset will interfere. A software power down - or reboot - will fire OnCloseQuery which gives you the opportunity to do some housekeeping. Note that if you are unfortunate enough to do File IO in finalization sections you are at a loss. Never do that! File IO in the OnCloseQuery event is much more safe.
This event is specifically designed to interact with the OS on shutdown. Anything else - in GUI applications - isn't.
With console applications you can hook into AddExitProc.
« Last Edit: October 30, 2018, 12:29:20 pm by Thaddy »
also related to equus asinus.

Sann-X!

  • New Member
  • *
  • Posts: 21
Re: gracefully close my app
« Reply #2 on: October 30, 2018, 12:44:59 pm »
OnCloseQuery doesn't work. I wrote code (save message into log file) in OnCloseQuery event, but it doesn't executed by OS shutdown. Log file doesn't exist. Unfortunately command 'kill -SIGINT <my_app_id>' works similarly.

Thaddy

  • Hero Member
  • *****
  • Posts: 9285
Re: gracefully close my app
« Reply #3 on: October 30, 2018, 12:54:05 pm »
kill is kill. That will kill any app regardless in which state. It is also a last resort. This will indeed not fire either the exitproc chain or OnCloseQuery.
A nice OS shutdown, however, should obey both. At least it does on Windows. I have to test linux. Wait a moment. I'll edit this after I tested on Linux too.
[edit]
Linux does not obey it. You are right.
« Last Edit: October 30, 2018, 01:07:50 pm by Thaddy »
also related to equus asinus.

Sann-X!

  • New Member
  • *
  • Posts: 21
Re: gracefully close my app
« Reply #4 on: October 30, 2018, 01:13:43 pm »
How to live with it?

Thaddy

  • Hero Member
  • *****
  • Posts: 9285
Re: gracefully close my app
« Reply #5 on: October 30, 2018, 01:44:52 pm »
How to live with it?
As most applications suffer the same fate, redesign your application. Example: maintain state on disk when vital parameters change. That way you can always revert to a valid state.
also related to equus asinus.

lucamar

  • Hero Member
  • *****
  • Posts: 2141
Re: gracefully close my app
« Reply #6 on: October 30, 2018, 03:42:28 pm »
The usual way to detect a close event is to set up a handler for SIGTERM with fpSigaction.

Do note, though, that some Linuxen send a SIGKILL inmediatly after the SIGTERM (example reference), which prevents your application from responding to the TERM signal.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Bart

  • Hero Member
  • *****
  • Posts: 3547
    • Bart en Mariska's Webstek
Re: gracefully close my app
« Reply #7 on: October 30, 2018, 05:12:56 pm »
Perhaps Application.OnQueryEndSession?

Bart

lucamar

  • Hero Member
  • *****
  • Posts: 2141
Re: gracefully close my app
« Reply #8 on: October 30, 2018, 06:02:19 pm »
Perhaps Application.OnQueryEndSession?

I tried that in a Linux application and it didn't work either: it was never called :'(
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 9285
Re: gracefully close my app
« Reply #9 on: November 01, 2018, 06:59:26 am »
Perhaps Application.OnQueryEndSession?

I tried that in a Linux application and it didn't work either: it was never called :'(
I believe the code is actually implemented correctly but I guess what happens is what you described before. SIGTERM and then immediately SIGKILL.
On such nixes I can't see anything that can be done. (except a work-around to maintain state for essentials in your software)
also related to equus asinus.

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1170
Re: gracefully close my app
« Reply #10 on: November 01, 2018, 07:31:19 am »
FPC has the FPSigaction function as stated before.

You can have a look at the mORMot on how to use it.
It has a file called mORMotService.

With code that looks like this:
Code: Pascal  [Select]
  1. fpSigaction(SIGQUIT, @saNew, @saOld);
  2. fpSigaction(SIGTERM, @saNew, @saOld);
  3. fpSigaction(SIGINT, @saNew, @saOld);
  4.  
Perhaps this can be of some help.
« Last Edit: November 01, 2018, 07:34:11 am by DonAlfredo »

Sann-X!

  • New Member
  • *
  • Posts: 21
Re: gracefully close my app
« Reply #11 on: November 01, 2018, 07:43:34 am »
FPC has the FPSigaction function as stated before.

You can have a look at the mORMot on how to use it.
It has a file called mORMotService.

With code that looks like this:
Code: Pascal  [Select]
  1. fpSigaction(SIGQUIT, @saNew, @saOld);
  2. fpSigaction(SIGTERM, @saNew, @saOld);
  3. fpSigaction(SIGINT, @saNew, @saOld);
  4.  
Perhaps this can be of some help.

Thnx. I have already implemented this. But I get new error if app receive SIG*: closing a firebird db connection raise a exception. And I can't catch this exception or debug it via gdb  :'(