Tuesday, January 12, 2010

Detect Lock/Unlock Status of a system

The following C# code will register for lock/unlock status notification and do the operation for corresponding status.

void AddLockStatusNotificationHandler()
{
Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(OnSessionSwitch);
}

void OnSessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
{
if (e.Reason == Microsoft.Win32.SessionSwitchReason.SessionLock)
{
//System Locked
}
else if (e.Reason == Microsoft.Win32.SessionSwitchReason.SessionUnlock)
{
//System Unlocked
}
}

No comments: