2

Imagine I was creating an application that measured how long a certain user is logged in to a computer (the actual application I'm writing is different, but that doesn't matter).

I need to track when the user logs in, logs out, when he suspends the computer (“stand by“ choice in the Shut Down menu) and when the computer resumes from sleep.

I am using SystemEvents (specifically, PowerModeChanged and SessionSwitch events) for that and it mostly works, with one exception:

When the computer is suspended, the PowerModeChanged event is correctly raised with the mode Suspend.

When the computer resumes, PowerModeChanged is raised with the mode Resume. So far so good.

Right after that, SessionSwitch event is raised with the reason SessionLock. But when I eventually log in after the resume, there is no SessionUnlock.

This behavior happens on Windows XP, and I need this to work correctly on XP, Vista and 7.

Is there a way how to receive this unlock?

svick
  • 236,525
  • 50
  • 385
  • 514
  • No, there's no documented way in Windows to detect a secure desktop becoming inactive. You have to poll. – Hans Passant Aug 10 '11 at 16:45
  • If you mean detecting whether the user is at the computer (and moves the mouse, presses keys), I don't care about that, just whether he is currently logged in, the computer is not suspended and the session is active. – svick Aug 10 '11 at 17:32

1 Answers1

3

Turns out there was an uncaught exception in one of my handlers. Apparently, this stopped subsequent events from firing.

svick
  • 236,525
  • 50
  • 385
  • 514
  • I was thinking about that, but I think it's possible someone might encounter the same problem, so having the answer here could be useful. – svick Aug 10 '11 at 17:30
  • @ian: svick is correct. This is a useful question which should not be deleted. – SLaks Aug 10 '11 at 17:57