I am trying to catch the session change events using .Net framework 4.7. According to Documentation , I have to override the OnSessionChange() method and change the CanHandleSessionChangeEvent to true but still I am unable to catch any event. Here is the code:
public Service1()
{
InitializeComponent();
CanHandleSessionChangeEvent = true;
this.ServiceName = "WindowsService1";
}
protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
// Handle session change event
switch (changeDescription.Reason)
{
case SessionChangeReason.SessionLogon:
// Code to execute when a user logs on
break;
case SessionChangeReason.SessionLogoff:
// Code to execute when a user logs off
break;
case SessionChangeReason.SessionLock:
// Code to execute when a session is locked
break;
case SessionChangeReason.SessionUnlock:
// Code to execute when a session is unlocked
break;
// Handle other session change reasons if needed
default:
break;
}
}
Same question has been asked on the stack overflow but no solution to this. Is there any issue in my code?
Can anybody please help!
I am expecting to catch the on session change when the computer is locked or unlocked!