I have an application where i need to periodically run some code in the background when the user is not logged in and then stop running the code when they log back into their windows session.
To do this i have initiated a simple timer on the SystemEvents.SessionSwitch
event like so:
Private Sub SystemEvent_SessionSwitched(sender As Object, e As Microsoft.Win32.SessionSwitchEventArgs)
WriteLogEntry("System Event Switch user Fired... initiating timer")
_switchTimer = New Timers.Timer(8000)
AddHandler _switchTimer.Elapsed, AddressOf OnSwitchTimedEvent
_switchTimer.AutoReset = True
_switchTimer.Enabled = True
End Sub
This is fine and works just how i need it to.
When the Timer elapses I then need to check to see if my user is logged back in, or if not carry out some background work. so i tried something this:
Private Sub OnSwitchTimedEvent(sender As Object, e As Timers.ElapsedEventArgs)
If Environment.UserName = Me.User.ID Then 'Me.User.ID stores the windows username of the person logged into my application
WriteLogEntry("User Logged back in... Resume Normal activity")
_switchTimer.Stop()
Exit Sub
Else
'Do the background stuff here
End If
End Sub
Based on the description of Environment.UserName
("Gets the user name of the person who is currently logged on to the Windows operating system.") I thought this would be perfect. However it seems that what Microsoft say and what they mean are two different things as it always returns the user who initiated the process of my application, which is not what I want.
For example if "User1" logs in and starts my application and then "User2" comes along and switches user to his/her windows account, Environment.UserName
still returns "User1". I need to work out how to return the active user (i.e "User2") in this example.
If anyone knows the correct property or even an alternative solution to finding out when the user of my application is active again it would be much appreciated.
edit: the end game is to see when my user has logged back in not to obtain the name... obtaining the name was the only way i could see to do it