Description of event SystemmEvents.SessionSwitch
Occurs when the currently logged-in user has changed.
The SessionSwitchEventArgs have a property Reason which tells you the reason of the event.
If remote desktop connects or disconnects, this event is raised.
- When starting remote desktop: Reason == SessionSwitchReason.RemoteConnect
- When remote desktop is closed: Reason == SessionSwitchReason.RemoteDisconnect
.
class ViewModel
{
public ViewModel()
{
SystemEvents.SessionSwitch += SessionSwitched;
IsRemoteSession = SystemParameters.IsRemoteSession;
Logger.LogTrace("ViewModel constructed. Remote Session {0}", this.IsRemoteSession);
}
public bool IsRemoteSession {get; set;}
private void SessionSwitched(object sender, SessionSwitchEventArgs e)
{
this.IsRemoteSession = SystemParameters.IsRemoteSession;
Logger.LogTrace("Session Switch. Reason: {0}; Remote Session {1}"
e.Reason, this.IsRemoteSession);
}
If I start the program without remote desktop, property IsRemoteSession is false. If I start remote desktop, the event is raised, but the property remains false.
However, if I start the program with remote descktop, IsRemoteSession is true. If I close remote desktop the event is raised, but the property remains true.
I also tried GetSystemMetrics, as suggested here, but that didn't give the correct value either, which could be expected, because SystemParameters uses GetSystemMetrics to get the values.