Detect Desktop after Resume from Sleep ...

Just starting out? Need help? Post your questions and find answers here.
escape75
User
User
Posts: 23
Joined: Fri Jan 09, 2026 7:52 am

Detect Desktop after Resume from Sleep ...

Post by escape75 »

Does anyone know how to easily detect when the Desktop is loaded after a wake up event ?

It seems the following code resumes while on the lock screen and certain Windows actions are not yet availabe until the user unlocks and goes into a Desktop session ?

Code: Select all


Enumeration 100
  #Window
  #Timer
EndEnumeration

Procedure Detect(hWnd, uMsg, wParam, lParam)
  If uMsg = #WM_POWERBROADCAST And wParam = #PBT_APMRESUMESUSPEND
    Debug Str(DateUTC()) + " - Wake1"
    AddWindowTimer(#Window, #Timer, 1000)
  EndIf
  
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

OpenWindow(#Window, 0, 0, 300, 70, "Test", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

SetWindowCallback(@Detect(), #Window)

Repeat
  Define Event = WaitWindowEvent()
  
  If Event = #PB_Event_Timer And EventTimer() = #Timer
    Debug Str(DateUTC()) + " - Wake2"
  EndIf
  
Until Event = #PB_Event_CloseWindow

User avatar
ChrisR
Addict
Addict
Posts: 1544
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Detect Desktop after Resume from Sleep ...

Post by ChrisR »

Maybe you can wait until the taskbar is created

Code: Select all

Global TB_Message = RegisterWindowMessage_("TaskbarCreated")

Enumeration 100
  #Window
  #Timer
EndEnumeration

Procedure Detect(hWnd, uMsg, wParam, lParam)
  If uMsg = TB_Message
    HideWindow(#Window, #False)
    Debug Str(DateUTC()) + " - TaskbarCreated1"
    AddWindowTimer(#Window, #Timer, 1000)
  EndIf
  
  ProcedureReturn #PB_ProcessPureBasicEvents  
EndProcedure

OpenWindow(#Window, 0, 0, 300, 70, "Test", #PB_Window_Invisible | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
SetWindowCallback(@Detect(), #Window)

Repeat
  Define Event = WaitWindowEvent()
  
  If Event = #PB_Event_Timer And EventTimer() = #Timer
    Debug Str(DateUTC()) + " - TaskbarCreated2"
  EndIf
  
Until Event = #PB_Event_CloseWindow
escape75
User
User
Posts: 23
Joined: Fri Jan 09, 2026 7:52 am

Re: Detect Desktop after Resume from Sleep ...

Post by escape75 »

I have tried it, but it only works when Windows restarts, as when it goes to sleep while logged in, the taskbar is already loaded :(
User avatar
Skipper
User
User
Posts: 71
Joined: Thu Dec 19, 2024 1:26 pm
Location: Europe

Re: Detect Desktop after Resume from Sleep ...

Post by Skipper »


Using PureBasic 6.21 on:
  • Win-11 on x64 XEON
  • Mint Linux on x64 i3 and on x64 i5
  • MacOS Monterey on x64 MacBook Pro
  • Various Raspberry Pi's

escape75
User
User
Posts: 23
Joined: Fri Jan 09, 2026 7:52 am

Re: Detect Desktop after Resume from Sleep ...

Post by escape75 »

I don't think it would help but I think I have found a solution via #WM_WTSSESSION_CHANGE & #WTS_SESSION_UNLOCK

Thanks!
User avatar
ChrisR
Addict
Addict
Posts: 1544
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Detect Desktop after Resume from Sleep ...

Post by ChrisR »

escape75 wrote: Mon Jan 12, 2026 9:27 pm ... but I think I have found a solution via #WM_WTSSESSION_CHANGE & #WTS_SESSION_UNLOCK
Yes, I tried it, it works after WTSRegisterSessionNotification(), but it seems to be only in administrator mode!

Code: Select all

Enumeration
  #Window
  #Wtsapi32Lib
  #Timer
EndEnumeration

#WTS_SESSION_LOCK   = $7
#WTS_SESSION_UNLOCK = $8

Prototype pWTSRegisterSessionNotification(hWnd, dwFlags)
Prototype pWTSUnRegisterSessionNotification(hWnd)

Procedure Detect(hWnd, uMsg, wParam, lParam)
  If uMsg = #WM_WTSSESSION_CHANGE 
    Select wParam
      Case #WTS_SESSION_LOCK
        Debug FormatDate("%hh:%ii:%ss", DateUTC()) + " - #WTS_SESSION_LOCK"
      Case #WTS_SESSION_UNLOCK
        Debug FormatDate("%hh:%ii:%ss", DateUTC()) + " - #WTS_SESSION_UNLOCK"
        AddWindowTimer(#Window, #Timer, 1000)
    EndSelect
  EndIf
  
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

OpenWindow(#Window, 0, 0, 300, 70, "Test", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

SetWindowCallback(@Detect(), #Window)

If OpenLibrary(#Wtsapi32Lib, "Wtsapi32.dll")
  Define WTSRegisterSessionNotification_.pWTSRegisterSessionNotification = GetFunction(#Wtsapi32Lib, "WTSRegisterSessionNotification")
  WTSRegisterSessionNotification_(WindowID(#Window), 0)
EndIf
 
Repeat
  Define Event = WaitWindowEvent()
  
  If Event = #PB_Event_Timer And EventTimer() = #Timer
    Debug FormatDate("%hh:%ii:%ss", DateUTC()) + " - Session Unlock"
    RemoveWindowTimer(#Window, #Timer) 
  EndIf
  
Until Event = #PB_Event_CloseWindow

If IsLibrary(#Wtsapi32Lib)
  Define WTSUnRegisterSessionNotification_.pWTSUnRegisterSessionNotification = GetFunction(#Wtsapi32Lib, "WTSUnRegisterSessionNotification")
  WTSUnRegisterSessionNotification_(WindowID(#Window))
  CloseLibrary(#Wtsapi32Lib)
EndIf
escape75
User
User
Posts: 23
Joined: Fri Jan 09, 2026 7:52 am

Re: Detect Desktop after Resume from Sleep ...

Post by escape75 »

Mine is similar code, and I only tested with a local admin account as that's what I've got setup:

Code: Select all


EnableExplicit

Enumeration 100
  #Window
EndEnumeration

#NOTIFY_FOR_THIS_SESSION = 0
#WM_WTSSESSION_CHANGE = $02B1
#WTS_SESSION_UNLOCK = $8

Import "wtsapi32.lib"
  WTSRegisterSessionNotification(hWnd, dwFlags)
  WTSUnRegisterSessionNotification(hWnd)
EndImport

Procedure Notify(hWnd, uMsg, wParam, lParam)
  If uMsg = #WM_WTSSESSION_CHANGE And wParam = #WTS_SESSION_UNLOCK
    Debug "Unlocked"
  EndIf
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

OpenWindow(#Window, 0, 0, 300, 70, "Wake Up Test", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
SetWindowCallback(@Notify(), #Window)
WTSRegisterSessionNotification(WindowID(#Window), #NOTIFY_FOR_THIS_SESSION)

Repeat
  Define Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow

WTSUnRegisterSessionNotification(WindowID(#Window))

User avatar
ChrisR
Addict
Addict
Posts: 1544
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Detect Desktop after Resume from Sleep ...

Post by ChrisR »

Similar but better, easier to read with the import :)
Post Reply