C# 为交换机用户处理系统事件

C# 为交换机用户处理系统事件,c#,events,system,handle,C#,Events,System,Handle,当用户切换到另一个用户时,我需要关闭串口并终止应用程序。当用户切换到另一个用户时是否可以处理 这是我的代码,我可以在用户注销时处理,但在切换用户时无法处理 static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e) { switch (e.Reason) { case SessionEndReasons.Logoff: serialPort1

当用户切换到另一个用户时,我需要关闭串口并终止应用程序。当用户切换到另一个用户时是否可以处理

这是我的代码,我可以在用户注销时处理,但在切换用户时无法处理

static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
    switch (e.Reason)
    {
        case SessionEndReasons.Logoff:
            serialPort1.Close();
            Process.GetCurrentProcess().Kill();
            break;

        //Here i want handle process for switch user
        //case SessiondEndReasons.SwitchUser??
        //      serialPort1.Close();
        //      Process.GetCurrentProcess().Kill();
        //      break;
    }
}

您必须触发代码以响应事件:

SystemEvents.SessionSwitch += (sender, args) =>
            {
                serialPort1.Close();
                Process.GetCurrentProcess().Kill();
            };
以下是您也可以处理的可能原因:

更多信息: