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.
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.
I may look like a mule, but I'm not a complete ass.
> 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
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:
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?
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.
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.
> 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
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?
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.
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