I want to write a service that detect system lock event.
Firstly, I do it with overriding the OnSessionChange method from the ServiceBase.
public SimpleService()
{
ServiceName = "Name";
CanStop = true;
CanPauseAndContinue = true;
AutoLog = true;
CanHandlePowerEvent = true;
CanShutdown = true;
CanHandleSessionChangeEvent = true;
}
protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
UpdateDatabase updateDatabase = UpdateDatabase.Instance;
switch (changeDescription.Reason)
{
case SessionChangeReason.SessionLock:
Console.WriteLine("Locked \r\n");
break;
case SessionChangeReason.SessionUnlock:
Console.WriteLine("Unlock \r\n");
break;
default:
break;
}
base.OnSessionChange(changeDescription);
}
But this one does not work.
Then I used the eventhandler SessionSwitchEventHandler from the SystemEvents, and it works.
static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
if(SessionSwitchReason.SessionLock == e.Reason)
{
Console.WriteLine("Locked \r\n");
}
}
SystemEvents.SessionSwitch += new sessionSwitchEventHandler(SystemEvents_SessionSwitch);
But I just dont understand why??? Could someone give me a hint about the difference between these two methods?
Comments
Post a Comment