Whilst I still maintain that this is user-hostile* and would strongly recommend you push back hard against whoever developed this requirement, what you're probably looking for is the WM_WTSSESSION_CHANGE
message. This is delivered to windows that have registered to receive it via the WTSRegisterSessionNotification
function.
What it reports are console/remote connection events and lock events. I believe that when your session is switched away from using fast user switching you'll receive the WTS_CONSOLE_DISCONNECT
. But note that there could be other reasons why you receive the event (for instance, because they were working locally and have now connected to their session via Remote Desktop). You'd have to decide whether it was acceptable to also kill your program in these other circumstances1.
This question looks for how to get window messages from within a WPF application.
*My first rule of thumb is that if you're being asked to do something based on how the OS itself is operating, first see if you can spot any other programs that act in the same way that you're proposing. If you cannot (I can't think of any that match your proposed way of working), you need to seriously consider whether a) your organisation is just the first one to come up with a fabulous idea that many others will copy in the future or b) it's plain bad UX. On almost all occasions, if you approach this question honestly, you'll realise it's (b).
1And this is just the first example I could think of off the top of my head. You're unlikely to find something that better fits your requirement because you're doing something that Microsoft would probably deeply frown upon, because it breaks the fast user switching experience.