最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

.net - Unable to catch PowerBroadcastStatus.Suspend and PowerBroadcastStatus.ResumeSuspend in Windows service using C# - Stack O

programmeradmin1浏览0评论

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.

Share Improve this question edited 9 hours ago marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked 11 hours ago Kristián NemčekKristián Nemček 111 bronze badge 3
  • 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
Add a comment  | 

1 Answer 1

Reset to default 0

You should consider reading events from the Event log, or use powercfg / sleepstudy to generate an html file with events you can parse out.

发布评论

评论列表(0)

  1. 暂无评论