It is currently Sat May 25, 2013 5:08 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Detects if windows is shutdown and closing your app
PostPosted: Sat Mar 19, 2011 2:23 am 
Offline
Addict
Addict
User avatar

Joined: Fri Apr 25, 2003 7:06 pm
Posts: 1979
Location: Argentina
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:
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


Top
 Profile  
 
 Post subject: Re: Detects if windows is shutdown and closing your app
PostPosted: Sat Mar 19, 2011 3:54 am 
Offline
Addict
Addict

Joined: Sat Jul 11, 2009 4:57 am
Posts: 878
Location: United States
You could check for shutdown.exe in the tasklist. I don't know if this would shorten the code though. :oops:

_________________
▓▓▓▓▓▒▒▒▒▒░░░░░


Top
 Profile  
 
 Post subject: Re: Detects if windows is shutdown and closing your app
PostPosted: Sat Mar 19, 2011 3:58 am 
Offline
Addict
Addict

Joined: Tue Feb 22, 2011 1:16 pm
Posts: 1460
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!


Top
 Profile  
 
 Post subject: Re: Detects if windows is shutdown and closing your app
PostPosted: Sat Mar 19, 2011 2:47 pm 
Offline
Addict
Addict
User avatar

Joined: Fri Apr 25, 2003 7:06 pm
Posts: 1979
Location: Argentina
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.


Top
 Profile  
 
 Post subject: Re: Detects if windows is shutdown and closing your app
PostPosted: Sun Jun 24, 2012 3:33 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri May 25, 2012 7:39 pm
Posts: 183
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?


Top
 Profile  
 
 Post subject: Re: Detects if windows is shutdown and closing your app
PostPosted: Sun Jun 24, 2012 4:38 pm 
Offline
Addict
Addict

Joined: Fri Oct 23, 2009 2:33 am
Posts: 2864
Location: Wales, UK
Something like this from Dige I think:
Code:
;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.


Top
 Profile  
 
 Post subject: Re: Detects if windows is shutdown and closing your app
PostPosted: Sun Jun 24, 2012 4:48 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri May 25, 2012 7:39 pm
Posts: 183
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 :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye