0

I want to close my application once switch user using Window+L key. Solution accepted in any language any technology.

I have one bookMyApp wpf application which is installed through MSI. Let suppose computer have multiple user and working with below scenario.

--> First user came open bookMyApp application and did some work, After some time he try to switch user without closing the bookMyApp application. At this time I want automatically close bookMyApp application. Is this possible by anyway?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112

1 Answers1

2

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.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • I understand your concern, will discuss all above points, thanks for your valuable time and effort – Gulam Husain Ansari Jul 18 '18 at 13:43
  • @GulamHusainAnsari - note that if your requirement was "if they attempt to launch our application again in a new user session we terminate the old copy", that wouldn't be nearly as *surprising* and does have precedents (various "single-use" licensing models follow this kind of pattern). But note that you'd be looking for a completely different solution to satisfy that. – Damien_The_Unbeliever Jul 18 '18 at 13:46
  • Now you got that, our requirement is "if they attempt to launch our application again in a new user session we terminate the old copy” and new user role not admin :). I did not get exact solution but you give me one way to make solution, I don't want to use win service :) – Gulam Husain Ansari Jul 18 '18 at 14:26
  • @GulamHusainAnsari You don't need to write a service for this. You just simply need to use an IPC mechanism that can cross session boundaries. The easiest way to detect a previous instance is to use a named kernel object, like an event or mutex, that is created in the `Global` namespace. If detected, send a message to the previous instance using a named pipe, mailslot, socket, etc, something is that not tied to any particular session – Remy Lebeau Jul 18 '18 at 22:05