gracefully close my app lazarus / 32-Linux ---------------------------------------------------------------------- Sann-X! · 2018-10-30 10:50 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 · 2018-10-30 11:12 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. ---------------------------------------------------------------------- Sann-X! · 2018-10-30 11:44 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 ' works similarly. ---------------------------------------------------------------------- Thaddy · 2018-10-30 11:54 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. ---------------------------------------------------------------------- Sann-X! · 2018-10-30 12:13 How to live with it? ---------------------------------------------------------------------- Thaddy · 2018-10-30 12:44 Quote from: Sann-X! on October 30, 2018, 01:13:43 pmHow 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. ---------------------------------------------------------------------- lucamar · 2018-10-30 14:42 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. ---------------------------------------------------------------------- Bart · 2018-10-30 16:12 Perhaps Application.OnQueryEndSession? Bart ---------------------------------------------------------------------- lucamar · 2018-10-30 17:02 Quote from: Bart on October 30, 2018, 05:12:56 pmPerhaps Application.OnQueryEndSession? I tried that in a Linux application and it didn't work either: it was never called ---------------------------------------------------------------------- Thaddy · 2018-11-01 05:59 Quote from: lucamar on October 30, 2018, 06:02:19 pmQuote from: Bart on October 30, 2018, 05:12:56 pmPerhaps 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) ---------------------------------------------------------------------- DonAlfredo · 2018-11-01 06:31 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i fpSigaction(SIGQUIT, @saNew, @saOld);fpSigaction(SIGTERM, @saNew, @saOld);fpSigaction(SIGINT, @saNew, @saOld); Perhaps this can be of some help. ---------------------------------------------------------------------- Sann-X! · 2018-11-01 06:43 Quote from: DonAlfredo on November 01, 2018, 07:31:19 amFPC 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i fpSigaction(SIGQUIT, @saNew, @saOld);fpSigaction(SIGTERM, @saNew, @saOld);fpSigaction(SIGINT, @saNew, @saOld); 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