Detecting suspend/hibernate [Resolved]

Just starting out? Need help? Post your questions and find answers here.
zoot33
User
User
Posts: 69
Joined: Sat Jul 11, 2009 7:43 pm
Location: Oxford, UK

Detecting suspend/hibernate [Resolved]

Post by zoot33 »

Hi can I detect that a PC is about to suspend or hibenate ?

Thanks.
Last edited by zoot33 on Tue Nov 23, 2010 12:18 pm, edited 1 time in total.
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Detecting suspend/hibernate

Post by PureLust »

I'm not sure if I get your concern right.
But if you're looking for the system idle time, the following code may help:

Code: Select all

EnableExplicit

Structure LASTINPUTINFO
  cbSize.l
  dwTime.l
EndStructure
  
Define Event
Define LastInput.LASTINPUTINFO
LastInput\cbSize = SizeOf(LASTINPUTINFO)

If OpenWindow(0,0,0,200,100,"User Idletime",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	
	AddWindowTimer(0,0,100)
	
	Repeat
		Event = WaitWindowEvent()
		
		If Event = #PB_Event_Timer
			GetLastInputInfo_(@LastInput)
			Debug "IdleTime: " + Str(ElapsedMilliseconds() - LastInput\dwTime) + " ms"
		EndIf
		
	Until Event = #PB_Event_CloseWindow
EndIf
Greetz, PL.
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
zoot33
User
User
Posts: 69
Joined: Sat Jul 11, 2009 7:43 pm
Location: Oxford, UK

Re: Detecting suspend/hibernate

Post by zoot33 »

Thanks. When a user decides to put their PC in standby, I want to do some stuff in my application before the PC goes to sleep. Therefore I need to detect when this is about to happen.
Last edited by zoot33 on Tue Nov 23, 2010 11:04 am, edited 1 time in total.
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Detecting suspend/hibernate

Post by PureLust »

zoot33 wrote:When a user decides to put their PC in standby, I want to do some stuff in my application before the PC goes to sleep. Therefore I need to detect when the PC is about to check out.
So I didn't get it right, the first time, sorry.
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
zoot33
User
User
Posts: 69
Joined: Sat Jul 11, 2009 7:43 pm
Location: Oxford, UK

Re: Detecting suspend/hibernate

Post by zoot33 »

no problem, thanks for your help.
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Detecting suspend/hibernate

Post by PureLust »

I assume, that the PBT_APMSUSPEND Event will be the thing you're looking for.
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
zoot33
User
User
Posts: 69
Joined: Sat Jul 11, 2009 7:43 pm
Location: Oxford, UK

Re: Detecting suspend/hibernate

Post by zoot33 »

I think it might be, do you have any examples of its use ?
zoot33
User
User
Posts: 69
Joined: Sat Jul 11, 2009 7:43 pm
Location: Oxford, UK

Re: Detecting suspend/hibernate

Post by zoot33 »

seems like i need to register my app to be able to receive those events
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Detecting suspend/hibernate

Post by PureLust »

zoot33 wrote:... , do you have any examples of its use ?
So far ... NO.

But I did not see a reason why I shouldn't write one: ;)

Code: Select all

; Suspend-Mode detection example.
; By Albert Cremers, (aka PureLust at PureBasic Forum) - 23.11.2010

EnableExplicit

Define Event

#PBT_APMQUERYSUSPEND		= $00
#PBT_APMSUSPEND			= $04
#PBT_APMRESUMESUSPEND	= $07

Procedure WinCallback(hWnd, uMsg, wParam, lParam) 
	Protected Counter
	
	If uMsg = #WM_POWERBROADCAST 
		Select wParam 
				
			Case	#PBT_APMQUERYSUSPEND
				Debug "Permission for Suspend requested ..."
				
			Case	#PBT_APMSUSPEND 
				Debug "Entering Suspend-Mode ..."
				Beep_(1350,200)
				Delay(20)
				Beep_(900,400)
				
			Case	#PBT_APMRESUMESUSPEND
				Debug "Awaken from Suspend"
				Delay(3000)
				Beep_(900,200)
				Delay(20)
				Beep_(1350,400)
				
		EndSelect 
	EndIf 
	
	ProcedureReturn #PB_ProcessPureBasicEvents 
EndProcedure 

If OpenWindow(0,0,0,200,100,"Waiting for Suspend-Mode ...",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	SetWindowCallback(@WinCallback())
	Repeat
		Event = WaitWindowEvent()
	Until Event = #PB_Event_CloseWindow
EndIf
(Example is working fine on my Win7-Laptop.)

Greetz, PL.
Last edited by PureLust on Tue Nov 23, 2010 12:46 pm, edited 3 times in total.
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
zoot33
User
User
Posts: 69
Joined: Sat Jul 11, 2009 7:43 pm
Location: Oxford, UK

Re: Detecting suspend/hibernate

Post by zoot33 »

thanks PL - it works great !
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Detecting suspend/hibernate [Resolved]

Post by PureLust »

Little improvement, with "Awaken from Suspend" detection (see code above).

(I hope, "Awaken from Suspend" is nearly right spelling. :| )

[Edit:] Maybe this Thread would be right placed in Tricks 'n' Tips now.
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
Post Reply