WM_QUERYENDSESSION how?

Just starting out? Need help? Post your questions and find answers here.
funnyguy
User
User
Posts: 69
Joined: Mon Jun 23, 2008 10:57 pm

WM_QUERYENDSESSION how?

Post 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 :)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: WM_QUERYENDSESSION how?

Post 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?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: WM_QUERYENDSESSION how?

Post 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.
I may look like a mule, but I'm not a complete ass.
funnyguy
User
User
Posts: 69
Joined: Mon Jun 23, 2008 10:57 pm

Post 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? :(
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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 
I may look like a mule, but I'm not a complete ass.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: WM_QUERYENDSESSION how?

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Aye, fair point. :)
I may look like a mule, but I'm not a complete ass.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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?
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: WM_QUERYENDSESSION how?

Post 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?
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: WM_QUERYENDSESSION how?

Post 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
Post Reply