Detects if windows is shutdown and closing your app

Share your advanced PureBasic knowledge/code with the community.
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Detects if windows is shutdown and closing your app

Post 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
ARGENTINA WORLD CHAMPION
Nituvious
Addict
Addict
Posts: 1027
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: Detects if windows is shutdown and closing your app

Post by Nituvious »

You could check for shutdown.exe in the tasklist. I don't know if this would shorten the code though. :oops:
▓▓▓▓▓▒▒▒▒▒░░░░░
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Detects if windows is shutdown and closing your app

Post 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.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Detects if windows is shutdown and closing your app

Post 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.
ARGENTINA WORLD CHAMPION
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: Detects if windows is shutdown and closing your app

Post 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?
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Detects if windows is shutdown and closing your app

Post 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 
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: Detects if windows is shutdown and closing your app

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