How to detect Windows session lock, unlock, logon and logoff events in a C# app
Use SystemEvents.SessionSwitch in a desktop or console app, override OnSessionChange in a Windows service, or hook WM_WTSSESSION_CHANGE yourself. Covers .NET 10 code for all three, the hidden '.NET System Events' thread, why services see logon but desktop apps do not, out-of-order notifications, and how to query the current lock state with WTSQuerySessionInformation.