Koray
Koray

Reputation: 407

Handling system event for switch user

I need to close serial port and kill application when user switch to another user. Is it possible to handle when user switch to another user?

Here is my code that i can handle when user log off but cannot handle when switch user.

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;
    }
}

Upvotes: 4

Views: 1870

Answers (1)

Nasreddine
Nasreddine

Reputation: 37798

You'll have to fire your code in response to the SystemEvents.SessionSwitch event instead:

SystemEvents.SessionSwitch += (sender, args) =>
            {
                serialPort1.Close();
                Process.GetCurrentProcess().Kill();
            };

Here are the possible reason you can handle too:

More info:

Upvotes: 6

Related Questions