SystemEvents.SessionSwitch Lock And UnLock Missing for few users.

babu

New member
Joined
Jun 16, 2020
Messages
3
Programming Experience
1-3
Team,

We are writing user events to a file in windows forms. This was now in production. For some users, if there is switch user or Remote user connection, UnLock and Lock events are not triggered and there is no exception. Please help me why UnLock and Lock are not triggered for witch user or Remote user connection.

C#:
private FileStream fileStream = new FileStream(@"c:\test.txt", FileMode.OpenOrCreate);

public static void RegisterForSystemEvents()
{
    SystemEvents.EventsThreadShutdown += new EventHandler(OnEventsThreadShutdown);
    SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPowerModeChanged);
    SystemEvents.SessionSwitch += new SessionSwitchEventHandler(OnSessionSwitch);
    SystemEvents.SessionEnding += new SessionEndingEventHandler(OnSessionEnding);
    SystemEvents.SessionEnded += new SessionEndedEventHandler(OnSessionEnded);
}

private static void OnSessionSwitch(object sender, SessionSwitchEventArgs e)
{
    // Check reason.
    switch (e.Reason)
    {
        case SessionSwitchReason.ConsoleConnect:
            WriteToFile("Session connected from the console");
            break;
        case SessionSwitchReason.ConsoleDisconnect:
            WriteToFile("Session disconnected from the console");
            break;
        case SessionSwitchReason.RemoteConnect:
            WriteToFile("Remote session connected");
            break;
        case SessionSwitchReason.RemoteDisconnect:
            WriteToFile("Remote session disconnected");
            break;
        case SessionSwitchReason.SessionLock:
            WriteToFile("Session has been locked");
            break;
        case SessionSwitchReason.SessionLogoff:
            WriteToFile("User was logged off from a session");
            break;
        case SessionSwitchReason.SessionLogon:
            WriteToFile("User has logged on to a session");
            break;
        case SessionSwitchReason.SessionRemoteControl:
            WriteToFile("Session changed to or from remote status");
            break;
        case SessionSwitchReason.SessionUnlock:
            WriteToFile("Session has been unlocked");
            break;
    }
}

private static void WriteToFile(string message)
{
    using (StreamWriter sw = File.AppendText(path))
    {
        sw.WriteLine(string.Format("Message : {0}; DateTime : {1}", message, DateTime.Now.ToString()));
    }
}
 
Last edited by a moderator:
insertcode.png
 
Occurs when the currently logged-in user has changed.
I read this as change notification only for current session, and not other sessions. A quick look at source code it appears to be using unmanged call WTSRegisterSessionNotification with NOTIFY_FOR_THIS_SESSION. For notification of other sessions you probably have to set up the unmanaged API yourself with NOTIFY_FOR_ALL_SESSIONS. See WTSRegisterSessionNotification function (wtsapi32.h) - Win32 apps and pinvoke.net: wtsregistersessionnotification (wtsapi32)
 
Back
Top Bottom