Page 1 of 1

Detect Desktop after Resume from Sleep ...

Posted: Sun Jan 11, 2026 9:23 pm
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


Re: Detect Desktop after Resume from Sleep ...

Posted: Sun Jan 11, 2026 10:34 pm
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

Re: Detect Desktop after Resume from Sleep ...

Posted: Sun Jan 11, 2026 11:17 pm
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 :(

Re: Detect Desktop after Resume from Sleep ...

Posted: Mon Jan 12, 2026 9:23 am
by Skipper

Re: Detect Desktop after Resume from Sleep ...

Posted: Mon Jan 12, 2026 9:27 pm
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!

Re: Detect Desktop after Resume from Sleep ...

Posted: Tue Jan 13, 2026 2:41 pm
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

Re: Detect Desktop after Resume from Sleep ...

Posted: Tue Jan 13, 2026 5:08 pm
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))


Re: Detect Desktop after Resume from Sleep ...

Posted: Tue Jan 13, 2026 5:23 pm
by ChrisR
Similar but better, easier to read with the import :)