title: SystemEvents and resuming from sleep
body: <p>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).</p>
  
  <p>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.</p>
  
  <p>I am using <a href="http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.aspx" rel="nofollow"><code>SystemEvents</code></a> (specifically, <a href="http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.powermodechanged.aspx" rel="nofollow"><code>PowerModeChanged</code></a> and <a href="http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionswitch.aspx" rel="nofollow"><code>SessionSwitch</code></a> events) for that and it mostly works, with one exception:</p>
  
  <p>When the computer is suspended, the <code>PowerModeChanged</code> event is correctly raised with the mode <code>Suspend</code>.</p>
  
  <p>When the computer resumes, <code>PowerModeChanged</code> is raised with the mode <code>Resume</code>. So far so good.</p>
  
  <p>Right after that, <code>SessionSwitch</code> event is raised with the reason <code>SessionLock</code>. But when I eventually log in after the resume, there is no <code>SessionUnlock</code>.</p>
  
  <p>This behavior happens on Windows XP, and I need this to work correctly on XP, Vista and 7.</p>
  
  <p>Is there a way how to receive this unlock?</p>