Forum > Linux

At power down ?

<< < (5/5)

LeP:

--- Quote from: dbannon on September 13, 2025, 05:16:34 am ---LeP, do you use TApplication.OnEndSession or do you handle the calls your self ?

--- End quote ---

This is the general code that I use since XP. Of course is on Windows. I do 99% Windows industrial applications. The GUI Interface is always present and it does only essential things like show something and interact with the user.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TFMain = class(TForm)     ......     protected        procedure QEndSession(var Msg: TMessage); message WM_QUERYENDSESSION;   end; implementation     function ShutdownBlockReasonCreate(hWnd: HWND; pwszReason: PWCHAR): BOOL; stdcall; external 'USER32.dll' name 'ShutdownBlockReasonCreate';    function ShutdownBlockReasonDestroy(hWnd: HWND): BOOL; stdcall; external 'USER32.dll' name 'ShutdownBlockReasonDestroy';         procedure TFMain.FormDestroy(Sender: TObject);    begin      //In this place is where all cleanup is done (generally speaking of course)      ...............      ...............      ShutdownBlockReasonDestroy(handle);    end;         procedure TFMain.QEndSession(var Msg: TMessage);  //WM_QUERYENDSESSION    begin      ExitCode := 0;      Msg.Result := 1;      ShutdownBlockReasonCreate(Handle,  'Delayed'); //THIS TEXT IS SHOW ON THE WINDOWS SHUTDOWN SCREEN IF THE APPLICATION SLOW DOWN THE PROCESS.      Close;    end; 

--- Quote from: Thaddy on September 13, 2025, 09:31:54 am ---(btw that code was written in Delphi, not Freepascal, although Freepascal compiles it nowadays)

--- End quote ---

Yes, it was done in Delphi and it is used in Delphi.


--- Quote from: Thaddy on September 13, 2025, 09:31:54 am ---I might add that a user interface is secondary to running a program. Delphi and Lazarus programmers often forget that:
It is the logic that performs, the GUI is ballast.

--- End quote ---

I'm in line with you, except that same applications (like mine)  needs GUI, so why not using some of their facilities ?
In my applications I use lot of external hardware and lot of TThreads, so I don't forget what the logic should do.

jamie:
For Windows users

https://learn.microsoft.com/en-us/windows/win32/power/wm-powerbroadcast

However, you may need to add some additional work to your form or Application to capture it, like hook the message loop.

 For that, I do have a helper unit that will easily assist you in capturing that message.

 For others that are not on windows, I don't know. I suppose a file monitor in a thread watching a log file could help trigger an alert.

Jamie

dbannon:
OK, here is my first cut at a Wiki page about the issues discussed in this thread.

https://wiki.freepascal.org/Shutdown_and_Powerdown

Much thanks to the contributors ! Please check out and flag any howlers you find.

Jamie, the things built into TApplication do seem to handle a Powerdown quite well,including Windows. What I have not addressed, and might well leave to people more familiar with Windows than me (and thats almost anyone) is when someone uses, eg, the Task Manager to kill an app.  In linux thats easy, just direct the 'signal' to a method that does what is needed. In Windows that is either WM_QUIT or WM_CLOSE but how to use them ?

LeP - I found no evidence of the 5 second limit you mention. But I did note that Windows gives you a dialog offering to kill a slow app after about (?) five seconds.

Davo

edit : typo, my keyboard is dying !

LeP:

--- Quote from: dbannon on September 15, 2025, 12:48:23 pm ---LeP - I found no evidence of the 5 second limit you mention.

--- End quote ---

I don't find the old reference about that (but I take a real applicaton locked and I press the power off button of a PC and this is the time that Windows wait for).
I remember it was related to shutdown procedure that Windows use at that now I know was changed over the time.

I have some win register keys where those times are exposed (there are other for services too), and they came from old defaults:

 - HKCU\Control Panel\Desktop\
   AutoEndtask
   WaittoKillAppTimeout
   HungAppTimeout

I tried to delete those and the effects on shutdown are the same. On the new systems these keys are not exposed, you must insert manually if you want to alter the times.
Every users can change the own times, depending on the applications.
By the way, these values were the default in the old times, and not enough for my applications. But with the API proposed Windows acts like I want.

I don't know if Windows, since those keys are not exposed anymore, acts in dinamic way for shutdown.

I will try with different machines and different configuration and loads to view how the shutdown acts.

But, since now what I have done is still working so I don't think to change it.


--- Quote from: dbannon on September 15, 2025, 12:48:23 pm ---LeP - But I did note that Windows gives you a dialog offering to kill a slow app after about (?) five seconds.

--- End quote ---

Yes, the screen propose the name of all applications that slow down the process and also the string that you use in the API I show in the posts.



--- Quote from: dbannon on September 15, 2025, 12:48:23 pm ---In Windows that is either WM_QUIT or WM_CLOSE but how to use them ?

--- End quote ---

WM_CLOSE message is sent to a FORM (or same controls) that should be closed. You can choose to really destroy or not the Form and all childs.
WM_QUIT is send to the Application, I don't know if you can intercept it normally or you must act in other way (I never catch WM_QUIT message).

There are more other messages the are sent to an application during the close process (like WM_DESTROY, WM_NCDESTROY, etc ..), but those no needs to catch too.

Navigation

[0] Message Index

[*] Previous page

Go to full version