Page 1 of 1

Detecting suspend/hibernate [Resolved]

Posted: Tue Nov 23, 2010 10:12 am
by zoot33
Hi can I detect that a PC is about to suspend or hibenate ?

Thanks.

Re: Detecting suspend/hibernate

Posted: Tue Nov 23, 2010 10:56 am
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.

Re: Detecting suspend/hibernate

Posted: Tue Nov 23, 2010 10:59 am
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.

Re: Detecting suspend/hibernate

Posted: Tue Nov 23, 2010 11:01 am
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.

Re: Detecting suspend/hibernate

Posted: Tue Nov 23, 2010 11:11 am
by zoot33
no problem, thanks for your help.

Re: Detecting suspend/hibernate

Posted: Tue Nov 23, 2010 11:40 am
by PureLust
I assume, that the PBT_APMSUSPEND Event will be the thing you're looking for.

Re: Detecting suspend/hibernate

Posted: Tue Nov 23, 2010 11:49 am
by zoot33
I think it might be, do you have any examples of its use ?

Re: Detecting suspend/hibernate

Posted: Tue Nov 23, 2010 12:03 pm
by zoot33
seems like i need to register my app to be able to receive those events

Re: Detecting suspend/hibernate

Posted: Tue Nov 23, 2010 12:05 pm
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.

Re: Detecting suspend/hibernate

Posted: Tue Nov 23, 2010 12:09 pm
by zoot33
thanks PL - it works great !

Re: Detecting suspend/hibernate [Resolved]

Posted: Tue Nov 23, 2010 12:42 pm
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.