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