I am trying to create a Windows service with .NET and C# that tracks a user's activity and measures how much time the user spends on a computer. I need to know when he logs in, locks the device, puts the device to sleep etc. The service then turns on/off a timer depending on the action and saves the period into a database.
For logging in, signing out, hibernating and turning the PC off, everything works fine, but I am unable to catch when I put the device to sleep and when it wakes up from sleep. I have tried to override OnPowerEvent
and checking what the value of PowerBroadcastStatus
is but that does not work for PowerBroadcastStatus.Suspend
and PowerBroadcastStatus.ResumeSuspend
. It works fine for PowerBroadcastStatus.PowerStatusChange
when I unplug the adapter or plug in for example.
PowerBroadcastStatus.Suspend
and PowerBroadcastStatus.ResumeSuspend
only work when the device is put to hibernate, not when put to sleep nor when it wakes up.
What I have noticed is that there are no logs in the event viewer under "System" for Kernel Power ID = 42 (Device going to sleep) and Power-Troubleshooter ID = 1 (Device waking up) when I put the pc to sleep or when it wakes up, so now I do not know whether it is a problem with my PC or code.
Furthermore, I have tried the SystemEvents
class but that did not work either and it did not work for SessionChange
before this problem.
The OnPowerEvent
function that should log the power event into the event viewer is:
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
{
base.OnPowerEvent(powerStatus);
EventLog.WriteEntry(SERVICENAME, powerStatus.ToString());
return base.OnPowerEvent(powerStatus);
}
CanHandlePowerEvent
is set to true in the constructor. I will be grateful for any tips.
I am trying to create a Windows service with .NET and C# that tracks a user's activity and measures how much time the user spends on a computer. I need to know when he logs in, locks the device, puts the device to sleep etc. The service then turns on/off a timer depending on the action and saves the period into a database.
For logging in, signing out, hibernating and turning the PC off, everything works fine, but I am unable to catch when I put the device to sleep and when it wakes up from sleep. I have tried to override OnPowerEvent
and checking what the value of PowerBroadcastStatus
is but that does not work for PowerBroadcastStatus.Suspend
and PowerBroadcastStatus.ResumeSuspend
. It works fine for PowerBroadcastStatus.PowerStatusChange
when I unplug the adapter or plug in for example.
PowerBroadcastStatus.Suspend
and PowerBroadcastStatus.ResumeSuspend
only work when the device is put to hibernate, not when put to sleep nor when it wakes up.
What I have noticed is that there are no logs in the event viewer under "System" for Kernel Power ID = 42 (Device going to sleep) and Power-Troubleshooter ID = 1 (Device waking up) when I put the pc to sleep or when it wakes up, so now I do not know whether it is a problem with my PC or code.
Furthermore, I have tried the SystemEvents
class but that did not work either and it did not work for SessionChange
before this problem.
The OnPowerEvent
function that should log the power event into the event viewer is:
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
{
base.OnPowerEvent(powerStatus);
EventLog.WriteEntry(SERVICENAME, powerStatus.ToString());
return base.OnPowerEvent(powerStatus);
}
CanHandlePowerEvent
is set to true in the constructor. I will be grateful for any tips.
- Isn't it software spying on employees? – Sergey A Kryukov Commented 11 hours ago
- Haha no, it will be a productivity app. – Kristián Nemček Commented 8 hours ago
- A productivity app? Don't you think that it is pretty much the same thing? :-) – Sergey A Kryukov Commented 6 hours ago
1 Answer
Reset to default 0You should consider reading events from the Event log, or use powercfg / sleepstudy to generate an html file with events you can parse out.