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;
};
#1 by Ben Swayne on June 25, 2010 - 8:57 am
Quote
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.
#2 by AAA on October 28, 2010 - 2:23 am
Quote
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 :/…
#3 by OmegaMan on October 28, 2010 - 7:35 am
Quote
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
#4 by nessimmax on March 18, 2012 - 6:08 pm
Quote
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;
}