Is my program still running, when another user is logged in (Windows)?

Everything else that doesn't fall into one of the other PB categories.
User avatar
jacdelad
Addict
Addict
Posts: 1993
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Is my program still running, when another user is logged in (Windows)?

Post by jacdelad »

Hi #PB_All,
I have a question regarding Windows: I have a Windows 10 computer with numerous accounts on it. User 1 logs in and starts a program written in PureBasic. Now, user 2 logs in (user 1 still logged in, not logged out, just locked the screeen via [Win]+[L]). What happens to the PureBasic program of user 1? It is still running, but is it still "working" in the background? Can the program detect, when another user is using the computer?
The background is that I want to kill the program after 5 minutes if the user is not logged in again within this timespan.
I hope I was able to explain my question...
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Is my program still running, when another user is logged in (Windows)?

Post by spikey »

jacdelad wrote: Wed May 03, 2023 2:14 pm It is still running, but is it still "working" in the background?
It still works providing you are using WindowEvent() or WaitWindowEvent() with a non-zero timeout value. If you don't use a timeout, the event loop will suspend at WaitWindowEvent() as soon as the session finishes switching out.
jacdelad wrote: Wed May 03, 2023 2:14 pm The background is that I want to kill the program after 5 minutes if the user is not logged in again within this timespan.
It would depend how sophisticated a behaviour you wanted to implement. If you can live with all sessions closing after a certain amount of inactivity then you could set a timeout of, say, 1000ms to WaitWindowEvent(). The result will be zero when the timeout occurs with no event. Count these up, if the count goes over your timeout, then exit. Reset the count whenever the result wasn't zero.

If you only want the action to apply when multiple sessions are running around it would get more complex, see below.
jacdelad wrote: Wed May 03, 2023 2:14 pm Can the program detect, when another user is using the computer?
I think you could use WM_WTSSESSION_CHANGE notifications but it's far from straightforward, because that screen you put your password into is not the same session as the one with your desktop on it, and nor is the one with a screensaver on it either apparently, if one is active.

You can register your application for these notifications using WTSRegisterSessionNotification().
See https://learn.microsoft.com/en-us/windo ... tification
and https://learn.microsoft.com/en-us/windo ... ion-change

The problem is that the sequence of events is not as simple as 'current session locks', 'new session starts'. Run this program, switch out and switch back and you'll see what I mean...

Code: Select all

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

Enumeration 0
  #NOTIFY_FOR_THIS_SESSION
  #NOTIFY_FOR_ALL_SESSIONS
EndEnumeration

Enumeration 1
  #WTS_CONSOLE_CONNECT
  #WTS_CONSOLE_DISCONNECT
  #WTS_REMOTE_CONNECT
  #WTS_REMOTE_DISCONNECT
  #WTS_SESSION_LOGON
  #WTS_SESSION_LOGOFF
  #WTS_SESSION_LOCK
  #WTS_SESSION_UNLOCK
  #WTS_SESSION_REMOTE_CONTROL
  #WTS_SESSION_CREATE
  #WTS_SESSION_TERMINATE
EndEnumeration

Procedure WinCallback(hWnd, uMsg, WParam, LParam) 
  ; Windows fills the parameter automatically, which we will use in the callback...
  
  Protected.S Message
  
  If uMsg = #WM_WTSSESSION_CHANGE
    
    Message = "Session : " + StrU(lParam) + " - "
    
    Select WParam
        
      Case #WTS_CONSOLE_CONNECT
        Message + "#WM_WTSSESSION_CHANGE - #WTS_CONSOLE_CONNECT"
        
      Case #WTS_CONSOLE_DISCONNECT
        Message + "#WM_WTSSESSION_CHANGE - #WTS_CONSOLE_DISCONNECT"
        
      Case #WTS_REMOTE_CONNECT
        Message + "#WM_WTSSESSION_CHANGE - #WTS_REMOTE_CONNECT"
        
      Case #WTS_REMOTE_DISCONNECT
        Message + "#WM_WTSSESSION_CHANGE - #WTS_REMOTE_DISCONNECT"
        
      Case #WTS_SESSION_LOGON
        Message + "#WM_WTSSESSION_CHANGE - #WTS_SESSION_LOGON"
        
      Case #WTS_SESSION_LOGOFF
        Message + "#WM_WTSSESSION_CHANGE - #WTS_SESSION_LOGOFF"
        
      Case #WTS_SESSION_LOCK
        Message + "#WM_WTSSESSION_CHANGE - #WTS_SESSION_LOCK"
        
      Case #WTS_SESSION_UNLOCK
        Message + "#WM_WTSSESSION_CHANGE - #WTS_SESSION_UNLOCK"
        
      Case #WTS_SESSION_REMOTE_CONTROL
        Message + "#WM_WTSSESSION_CHANGE - #WTS_SESSION_REMOTE_CONTROL"
        
      Case #WTS_SESSION_CREATE
        Message + "#WM_WTSSESSION_CHANGE - #WTS_SESSION_CREATE"
        
      Case #WTS_SESSION_TERMINATE
        Message + "#WM_WTSSESSION_CHANGE - #WTS_SESSION_TERMINATE"
        
    EndSelect
    
    AddGadgetItem(0, -1, Message)
    
  EndIf
  
  If uMsg = #WM_SIZE 
    Select WParam 
      Case #SIZE_MINIMIZED 
        Message = "Window was minimized" 
      Case #SIZE_RESTORED 
        Message = "Window was restored" 
      Case #SIZE_MAXIMIZED 
        Message = "Window was maximized" 
    EndSelect
    AddGadgetItem(0, -1, Message)
    
  EndIf 
  
  ProcedureReturn #PB_ProcessPureBasicEvents 
EndProcedure 


If OpenWindow(0, 0, 0, 500, 400, "Messages", #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget) 
  ListViewGadget(0, 5, 5, 490, 390)
  
  WTSRegisterSessionNotification(WindowID(0), #NOTIFY_FOR_ALL_SESSIONS)
  
  SetWindowCallback(@WinCallback(), 0) ; set the callback
  
  Repeat 
    Select WaitWindowEvent() 
      Case #PB_Event_CloseWindow 
        Break 
    EndSelect 
  ForEver 
  
  WTSUnRegisterSessionNotification(WindowID(0))
  
EndIf 
User avatar
jacdelad
Addict
Addict
Posts: 1993
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Is my program still running, when another user is logged in (Windows)?

Post by jacdelad »

Hello spikey,
what would we do without you in this forum? I always love when you respond, thanks für taking time!

I just tested your code and it seems to work perfectly: I receive a message when locking the computer and one when logging back in. That is all I need. Thanks very much! I'll also take a look into the documents from Microsoft, there's sure a lot more that can be done.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Is my program still running, when another user is logged in (Windows)?

Post by spikey »

Glad I could help! Thank you for your kind words :D
Post Reply