Rajesh Pawde
Rajesh Pawde

Reputation: 399

How to handle switch user in c#?

I want to do a set of task in my windows forms application when user switches from one account to other using the Switch user option provided by windows 7.So do we have some system event to handle this switch user case?

Upvotes: 2

Views: 2571

Answers (2)

DSander
DSander

Reputation: 1124

I know this is old, but I didn't see a helpful answer. You do need to handle the SystemEvents.SessionSwitch event. When a Windows user switches to another user, the SessionSwitchEventArgs from the event will have a Reason property that is a SessionSwitchReason enum with the value of "ConsoleDisconnect". When the user switches back, it should have a value of "ConsoleConnect".

Upvotes: 1

Patrick Hofman
Patrick Hofman

Reputation: 156948

I think SystemEvents.SessionSwitch is what you need. You can listen to a new user logging in and count that as a user switch, since your app is still active (else you would have a logoff first).

Occurs when the currently logged-in user has changed.

I guess the logic for a user switch is:

  • The current session is still alive (didn't logoff);
  • Another session logs in or unlocks.

Both events can be handled by this event.

Upvotes: 4

Related Questions