Page 1 of 2
WM_QUERYENDSESSION how?
Posted: Fri Mar 13, 2009 11:18 am
by funnyguy
Hi
My app users have complained that my app is not saving the settings when they shut down the computer when the apps running (a clock). The CloseWindow triggers the saving but it seems the close window message is not sent when windows is shutting down. So I went on the internet and found that the app will get a WM_QUERYENDSESSION message when its shutting down. But how do I know I got that message? And MSDN says something about return a value TRUE/FALSE.. please help me with this.
Thanks

Re: WM_QUERYENDSESSION how?
Posted: Fri Mar 13, 2009 11:22 am
by PB
Someone here will surely show you how to do it with a callback, but my
question is: why isn't your app saving changes as soon as they occur?
Posted: Fri Mar 13, 2009 11:23 am
by srod
You're probably better off with #WM_ENDSESSION in your case.
Use a Window callback and trap #WM_ENDSESSION. Process it in the same you do when receiving a #PB_Event_CloseWindow etc.
Re: WM_QUERYENDSESSION how?
Posted: Fri Mar 13, 2009 11:24 am
by srod
PB wrote:Someone here will surely show you how to do it with a callback, but my
question is: why isn't your app saving changes as soon as they occur?
My current app only saves settings on program close; things like positions of splitters etc. It would be very inefficient to save these values everytime the user adjusted a splitter etc.
Posted: Fri Mar 13, 2009 11:35 am
by funnyguy
srod wrote:You're probably better off with #WM_ENDSESSION in your case.
Use a Window callback and trap #WM_ENDSESSION. Process it in the same you do when receiving a #PB_Event_CloseWindow etc.
Exactly! How do I do it?

Posted: Fri Mar 13, 2009 11:55 am
by srod
Code: Select all
Procedure WinCallback(hWnd, uMsg, wParam, lParam)
Protected result = #PB_ProcessPureBasicEvents
Select uMsg
Case #WM_ENDSESSION, #WM_CLOSE
Debug "Session/window ending, better do some tidying up......"
If uMsg = #WM_ENDSESSION
result = 0
EndIf
EndSelect
ProcedureReturn result
EndProcedure
If OpenWindow(0, 0, 0, 400, 400, "#WM_ENDSESSION", #PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered)
SetWindowCallback(@WinCallback())
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
End
EndSelect
ForEver
EndIf
Re: WM_QUERYENDSESSION how?
Posted: Fri Mar 13, 2009 12:36 pm
by PB
> It would be very inefficient to save these values everytime the user
> adjusted a splitter etc
True, but I didn't really mean with every little change like that. I meant
when a preference box was adjusted and then closed, like when changing
a PureBasic IDE setting. Apps that only save prefs when they exit, run the
risk of losing those changes if the app crashes.
Posted: Fri Mar 13, 2009 12:41 pm
by srod
Aye, fair point.

Posted: Fri Mar 13, 2009 12:56 pm
by Rescator
WM_QUERYENDSESSION is a message that Windows sends to all apps, asking if the app is prepared to shutdown in a moment.
If you return false then the app could prevent the shutdown, this is something I'm using in a app I'm working on:
(from the window callback)
Code: Select all
Case #WM_QUERYENDSESSION
;If lParam&$80000000 then we are not logging off but shutting down.
If var\config_recording
result=#False ;We are recording, indicate we can not quit properly upon getting WM_ENDSESSION
Else
result=#True ;We are able to quit properly upon getting WM_ENDSESSION
EndIf
;Might be wise to start a background save of any program progress at this stage too, if needed, or query the user if appropriate
Case #WM_ENDSESSION
Program_Settings_Save() ;this really need to be quick, if it takes too long Windows might kill us before we're done.
PS! Hey Fred, does Linux and Mac have something similar? If so then this would be very cool for PureBasic to support, it would make for easy and clean shutdown of apps across platforms?
Posted: Fri Mar 13, 2009 1:01 pm
by PB
> Program_Settings_Save() ;this really need to be quick, if it takes too
> long Windows might kill us before we're done
Which is precisely why an app should save when changes occur, rather
than trying to rush everything in at the very last minute.

Posted: Fri Mar 13, 2009 1:08 pm
by Rescator
Yup. And I do, but stuff like window position etc. is something I do not store at each window "move", except at closing the program so it needs to be done at that point. Luckily it's very quick though.
Posted: Fri Mar 13, 2009 1:12 pm
by Rescator
Tip! If your program has an unsaved document and the system is shuting down or logging out the proper way (per the SDK) is return false wehen you get the QUERY message to prevent windows from shutting down, then pop up a box and ask the user if they wish to save the document. Once that is done and they try to shut down again you' obviously return true to the QUERY and let windows actually shut down.
So many programs either get all this wrong or not at all, odd considering how simple this really is thanks to those two messages.
Posted: Fri Mar 13, 2009 1:23 pm
by PB
> stuff like window position etc. is something I do not store at each window "move"
That's not what I meant, nor what I do either.
Anyway, on this subject: you see a tip for Microsoft Windows on many
web sites where you alter the shutdown delay from 20 seconds to only
1, because it lets you shut down faster. What newbies don't realise by
doing this, is that their running apps won't have time to save their data.
That's why I always recommend saving soon after a change, such as
when a preference box closes, etc. Not just when the app exits because
you don't know what shutdown delay the user has set, and saving at
exit means the user could terminate the app (via the Task Manager)
first and your app will not exit cleanly and save the changes.
Re: WM_QUERYENDSESSION how?
Posted: Sun Jun 17, 2012 3:02 pm
by J@ckWhiteIII
This post seems to be a little old, but still I have a question:
Win7 automatically tells you how many apps are running and (in case one does) which one keeps windows from shutting down. But how can I prevent this? I tried it with a messagerequester before the "result = #True", but it didn't help. Does anyone know how to do that?
Re: WM_QUERYENDSESSION how?
Posted: Sat Mar 14, 2015 1:41 am
by Dude
This old doesn't work with Windows 7, but did on previous versions of Windows. Any ideas on how to fix it? I want to prevent a logoff, shutdown, or reboot of my PC until my app is done.
It seems to be something new starting with Vista:
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
Code: Select all
Global nope
Procedure MyCallback(WindowID,Message,wParam,lParam)
Result=#PB_ProcessPureBasicEvents
If Message=#WM_QUERYENDSESSION
nope=1
Result=0 ; Aborts shutdown.
EndIf
ProcedureReturn Result
EndProcedure
OpenWindow(0,200,200,300,100,"Try to shutdown...",#PB_Window_SystemMenu)
SetWindowCallback(@MyCallback())
Repeat
ev=WindowEvent() : If ev=0 : Sleep_(1) : EndIf
If nope=1
nope=0
MessageRequester("Shut down aborted","Nope, I won't allow it!")
EndIf
Until ev=#PB_Event_CloseWindow