C#: How to Detect if a Workstation has been Locked and Unlocked

Posted by OmegaMan at February 23, 2010

Category: How To

Tags:

Here is a quick little console program which demonstrates how to detect if a computer has been either locked or unlocked using C#.  We simply subscribe to the SessionSwitchEventHandler Delegate for the lock or unlock reasons.

// using Microsoft.Win32;
public class CheckForWorkstationLocking : IDisposable
{
    private SessionSwitchEventHandler sseh;
    void SysEventsCheck( object sender, SessionSwitchEventArgs e )
    {
        switch ( e.Reason )
        {
            case SessionSwitchReason.SessionLock: Console.WriteLine( "Lock Encountered" ); break;
            case SessionSwitchReason.SessionUnlock: Console.WriteLine( "UnLock Encountered" ); break;
        }
    }

    public void Run()
    {
        sseh = new SessionSwitchEventHandler( SysEventsCheck );
        SystemEvents.SessionSwitch += sseh;
    }


    #region IDisposable Members

    public void Dispose()
    {
        SystemEvents.SessionSwitch -= sseh;
    }

    #endregion
}

Here is a the code in a console application code to run it:

CheckForWorkstationLocking workLock = new CheckForWorkstationLocking();

workLock.Run();

Console.WriteLine("Press ESC to exit...");
while ( true )
{
    ConsoleKeyInfo key = Console.ReadKey( true );
    if ( key.Key == ConsoleKey.Escape )
        break;
};
Share

5 Comments

  1. Ben Swayne says

    Excellent snippet of code! This is exactly what I needed for a POS application which should release POS Hardware when the station is locked, then re-acquire the hardware when unlocked. Works perfectly.

    Reply
  2. AAA says
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Win32;
    
    namespace Lock_Check
    {
        class Program
        {
            static void Main( string[] args )
            {
    
                CheckForWorkstationLocking workLock = new CheckForWorkstationLocking();
    
                workLock.Run();
    
                Console.WriteLine( "Press ESC to exit…" );
                while ( true )
                {
                    ConsoleKeyInfo key = Console.ReadKey( true );
                    if ( key.Key == ConsoleKey.Escape )
                        break;
                };
    
            }
        }
    }
    
    // using Microsoft.Win32;
    public class CheckForWorkstationLocking : IDisposable
    {
        private SessionSwitchEventHandler sseh;
        private SessionEndingEventHandler sseh2;
        void SysEventsCheck( object sender, SessionSwitchEventArgs e )
        {
            switch ( e.Reason )
            {
                case SessionSwitchReason.SessionLock: Console.WriteLine( "Lock Encountered" ); break;
                case SessionSwitchReason.SessionUnlock: Console.WriteLine( "UnLock Encountered" ); break;
            }
        }
    
        void SysEventsCheck2( object sender, SessionEndingEventArgs e )
        {
            switch ( e.Reason )
            {
                default: Console.WriteLine( "Lock Encountered" ); break;
            }
    
        }
    
        public void Run()
        {
            sseh = new SessionSwitchEventHandler( SysEventsCheck );
            SystemEvents.SessionSwitch += sseh;
    
            sseh2 = new SessionEndingEventHandler( SysEventsCheck2 );
            SystemEvents.SessionEnding += sseh2;
        }
    
        #region IDisposable Members
    
        public void Dispose()
        {
            SystemEvents.SessionSwitch -= sseh;
        }
    
        #endregion
    }
    

    Doesn’t work… Why :)? Even your “clear” version :/…

    Reply
    • OmegaMan says

      I cut and pasted it into VS 2010 console application as is; it worked finding a lock and unlock event on Win 7. Sorry AAA

      Reply
  3. nessimmax says

    void SysEventsCheck(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
    {
    switch (e.Reason)
    {
    case Microsoft.Win32.SessionSwitchReason.SessionLock: Console.WriteLine(“Lock Encountered”); break;
    case Microsoft.Win32.SessionSwitchReason.SessionUnlock: Console.WriteLine(“UnLock Encountered”); break;
    }
    }

    public void Run()
    {
    sseh = new Microsoft.Win32.SessionSwitchEventHandler(SysEventsCheck);
    Microsoft.Win32.SystemEvents.SessionSwitch += sseh;
    }

    public void Dispose()
    {
    Microsoft.Win32.SystemEvents.SessionSwitch -= sseh;
    }

    Reply
  4. shimon says

    very good !!1

    Reply

Leave a comment

(required)
(required) (will not be published)

This site uses Akismet to reduce spam. Learn how your comment data is processed.