C# SessionSwitchReason.SessionLock NOT triggering when machine is locked via Group Policy
<p><strong>EDIT: The issue here wan't the fact it was locked via GP, it was that it was being run as a service under a service account and it didn't have access to the interactive desktop</strong></p>

<p>I have a C# application that needs to check for when a user's session is locked, I'm using <code>Microsoft.Win32.SystemEvents.SessionSwitch</code> and this works fine when a user manually locks the machine. </p>

<p>The problem is that when the machine is locked via a group policy (User Configuration > Policies > Administrative Templates > Personalization > Screen saver timeout) the application doesn't pick up the switch.</p>

<p>Is there another way to check for a machine being locked? Or is there another way to lock machines via group policy that will be picked up by the application?</p>

<p>N.B. The application is running on windows 7 as a service with full admin rights</p>

<p>Here's my code, Thanks in advance!!! :)</p>

<pre><code>     public void OnStart(string[] args)
            {

            Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);
            }


    void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
           {

            if (e.Reason == SessionSwitchReason.SessionLock)
                  {
                   //DO STUFF
                  }
           }
</code></pre>