CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2004
    Posts
    561

    Does anyone know how to capture PC locked events?

    I've tried looking at SystemEvents but I don't see anything that has to do with capturing an event if the user locks the PC. Does such an event handler exist in C#?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Does anyone know how to capture PC locked events?

    Looks like it:
    Code:
    using System;
    using Microsoft.Win32;
    
    public class App
    {
         static void Main()
      { 
    
         SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
         Console.ReadLine(); // block main thread
    // !! detach the eventhandler when the application terminates, or you will
    leak a handle.
         SystemEvents.SessionSwitch -= SystemEvents_SessionSwitch;
      }
    
         static void SystemEvents_SessionSwitch(object sender,   SessionSwitchEventArgs e)
      {
        if(e.Reason == SessionSwitchReason.SessionLock)
            {...}
      }
    }
    
    }
    http://bytes.com/forum/thread536508.html
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Sep 2004
    Posts
    561

    Re: Does anyone know how to capture PC locked events?

    Quote Originally Posted by dglienna
    Looks like it:
    Code:
    using System;
    using Microsoft.Win32;
     
    public class App
    {
    static void Main()
    { 
     
    SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
    Console.ReadLine(); // block main thread
    // !! detach the eventhandler when the application terminates, or you will
    leak a handle.
    SystemEvents.SessionSwitch -= SystemEvents_SessionSwitch;
    }
     
    static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
    {
    if(e.Reason == SessionSwitchReason.SessionLock)
    {...}
    }
    }
     
    }
    http://bytes.com/forum/thread536508.html

    Awesome! Thank you, I cannot believe the answer was right under my nose. Still having some problems with this as the events do not get generated but I'm taking a look at the website link you gave for more clues. I think I'll find an answer soon. Thank you!

  4. #4
    Join Date
    Oct 2008
    Posts
    3

    Re: Does anyone know how to capture PC locked events?

    Did you every solve your issue of not capturing the events. I am having the same issue.

    It has been a long day so I might be missing something. I will take another look in the morning.
    Last edited by cewolcott; October 13th, 2008 at 02:28 PM.

  5. #5
    Join Date
    Jun 2011
    Posts
    4

    Re: Does anyone know how to capture PC locked events?

    Windows service not able to capture Login/Lock/Unlock in Win7.
    if any one knows the solution or any idea or approach, please help me out.
    waiting for all of your positive response.

    Thanks in Advance..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured