How to get windows unlock event in windows c # application?

I want to track a Windows unlock event in a Windows application. How it's done? What event is used for this? Do I need to import any namespace for this?

While the user opens the windows, the application must perform some tasks.

+5
source share
1 answer

As stated in StackOverflow's answer: fooobar.com/questions/59430 / ... , you should take a look at the SystemEvents.SessionSwitch Event .

Sample code can also be found in the answer.

, fooobar.com/questions/59430/..., , , , RTM Windows 8 .NET framework 4.5.

, .

using System;
using Microsoft.Win32;

// Based on: /questions/59430/how-can-i-programmatically-determine-if-my-workstation-is-locked/412308#412308
namespace WinLockMonitor
{
    class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);
            Console.ReadLine();
        }

        static void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
        {
            if (e.Reason == SessionSwitchReason.SessionLock)
            {
                //I left my desk
                Console.WriteLine("I left my desk");
            }
            else if (e.Reason == SessionSwitchReason.SessionUnlock)
            {
                //I returned to my desk
                Console.WriteLine("I returned to my desk");
            }
        }
    }
}
+11

All Articles