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...
Is my program still running, when another user is logged in (Windows)?
Is my program still running, when another user is logged in (Windows)?
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
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: Is my program still running, when another user is logged in (Windows)?
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 It is still running, but is it still "working" in the background?
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.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.
If you only want the action to apply when multiple sessions are running around it would get more complex, see below.
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.jacdelad wrote: Wed May 03, 2023 2:14 pm Can the program detect, when another user is using the computer?
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
Re: Is my program still running, when another user is logged in (Windows)?
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.
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
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: Is my program still running, when another user is logged in (Windows)?
Glad I could help! Thank you for your kind words 
