Page 1 of 1

Detects if windows is shutdown and closing your app

Posted: Sat Mar 19, 2011 2:23 am
by ricardo
In one application of mine, its important to distinguish if the user close the app from windows shutdown, because in the second case i need to do some stuff before closing.

Here is my code to do that. Notice that you can do some quick stuff after reciving the #WM_QUERYENDSESSION

Run this code and restar or shutdown windos and you will see:

Code: Select all

Procedure WinCallback(hWnd, uMsg, wParam, lParam) 
  
    If uMsg = #WM_QUERYENDSESSION
		;Do your stuff here
		MessageRequester("","Oops! Windows are shuting down!!")
	EndIf 
	
    ProcedureReturn #PB_ProcessPureBasicEvents 
EndProcedure 

If OpenWindow(0,100,150,450,200,"Windows shutdown detect",#PB_Window_SystemMenu)
	SetWindowCallback(@WinCallback())    
	
	CreateGadgetList(WindowID(0))
	StringGadget(1,50,50,350,20,"")
	ButtonGadget(2,200,100,150,25,"Do nothing yet")
	Repeat
		eventID=WaitWindowEvent()
		
		Select eventID
				
			Case #PB_Event_Gadget
				Select EventGadget()
					Case 2
				EndSelect
				
		EndSelect
		
	Until eventID=#PB_Event_CloseWindow
EndIf

Re: Detects if windows is shutdown and closing your app

Posted: Sat Mar 19, 2011 3:54 am
by Nituvious
You could check for shutdown.exe in the tasklist. I don't know if this would shorten the code though. :oops:

Re: Detects if windows is shutdown and closing your app

Posted: Sat Mar 19, 2011 3:58 am
by MachineCode
Doesn't exist in XP, and it's the wrong way to do it anyway. It could be renamed to something else, for example.

Re: Detects if windows is shutdown and closing your app

Posted: Sat Mar 19, 2011 2:47 pm
by ricardo
Nituvious wrote:You could check for shutdown.exe in the tasklist. I don't know if this would shorten the code though. :oops:
Not sure.
However, i think the appropiate way is to wait for WM_QUERYENDSESSION.
Its much less CPU consuming also.

Re: Detects if windows is shutdown and closing your app

Posted: Sun Jun 24, 2012 3:33 pm
by J@ckWhiteIII
Question: I read you could also examine if Windows is sending a message to all apps asking if they are ready for the shutdown process. I also read, that you can return #False si Windows stops the shutdown process. But in this case, Windows says that one app keeps the system from shutting down and asks me to close the app. But actually, I want the app to ask the user if he wants to save changes before the shutdown process (meaning Windows will have to wait till the user has either saved or quit without saving). Is there any possibility to code this?

Re: Detects if windows is shutdown and closing your app

Posted: Sun Jun 24, 2012 4:38 pm
by IdeasVacuum
Something like this from Dige I think:

Code: Select all

;German forum: http://www.purebasic.fr/german/archive/viewtopic.php?t=2328&highlight =
;  Author: dige (updated for PB4.00 by blbltheworm)
;  Date: 18 September 2003
;  OS: Windows
;  Demo: No.

;  explanation:
;  When Windows is shut down by the user or the user is
;  logs and this is not done with the FORCE option, will at all
;  running processes a # WM_QUERYENDSESSION message sent.
; 
;  This is answered in # True or False #. If, however, a
;  # Pass True, the shutdown process is canceled completely!
; 
;  This can be used to clean a program to end early
;  or intercept the shutdown process ...

Procedure.b TerminateProgram() 
  Shared Quit.b 
  ;  Called at Windows shutdown
  If MessageRequester( "Windows Shutdown", "Save changes before Windows to Exit?", #MB_ICONEXCLAMATION | #MB_YESNO ) = 6 
 
   ;-->>>>Add your Save Changes code here<<<<--
   Quit.b = 1 
    ProcedureReturn #True 
  Else 
    ProcedureReturn #False 
  EndIf 
EndProcedure 

Procedure WindowsCallback(WindowID, Message, wParam, lParam)  
  If Message = #WM_QUERYENDSESSION 
    ProcedureReturn TerminateProgram() 
  Else 
    ProcedureReturn #PB_ProcessPureBasicEvents 
  EndIf    
EndProcedure 

OpenWindow( 0,90,90,300,100, "Waiting for Windows Shutdown" ,#PB_Window_SystemMenu) 

SetWindowCallback(@WindowsCallback())  

Repeat 
  Event = WindowEvent() 
  Delay ( 100 ) 
Until Quit.b Or Event = #PB_Event_CloseWindow 

Re: Detects if windows is shutdown and closing your app

Posted: Sun Jun 24, 2012 4:48 pm
by J@ckWhiteIII
Ah yeah I see, of course the #True constant is returned AFTER the save changes code was run - *facepalm*
Thanks for the reply, should've thought about my question in a more open-minded way :)