C# - Raise event on PowerStatus change(C# - 在 PowerStatus 更改时引发事件)
问题描述
我创建了一个需要处于安全状态的应用程序,因此我想在后台跟踪计算机的电源状态.如果电池电量(如果有)低或严重,我将不允许用户继续使用应用程序并正常退出.
I've created an application that need to be in a safe state and so I want to follow the power status of the computer in background. If the battery level (if any) is low or critical, I wouldn't allow the user to continue using the application and quit all properly.
首先,我很惊讶不存在这样的事件来检测变化.您需要始终手动检查 PowerStatus.
First of all, I'm astonished that no such event exists in order to detect the change. You need always to check the PowerStatus manually.
所以,我围绕它创建了一个包装器,如下所示:
So, I've created a wrapper around it, something like this :
using System;
using System.Windows.Forms;
namespace MyApp
{
internal static class BatteryManagement
{
//
internal static event EventHandler<EventArgs> Changed;
//
private static bool _started;
private static System.Threading.Timer _timer;
private static PowerStatus _previousPowerStatus;
internal static void Start()
{
if (!_started)
{
_started = true;
ManageBatteryLevel();
}
}
internal static void Stop()
{
if (_started)
{
if(_timer != null)
{
_timer.Dispose();
_timer = null;
}
_started = false;
}
}
private static void ManageBatteryLevel()
{
_previousPowerStatus = new PowerStatus();
TimeSpan dueTime = new TimeSpan(0, 0, 0); // Start right now
TimeSpan period = new TimeSpan(0, 1, 0); // Run every 1 minute
// Setting a timer that launch every period the OnBatteryLevelChange method
_timer = new System.Threading.Timer(OnBatteryLevelChange, null, dueTime, period);
}
private static void OnBatteryLevelChange(Object stateInfo)
{
PowerStatus powerStatus = new PowerStatus();
if (!_previousPowerStatus.Equals(powerStatus))
{
// Ensure battery level is up to date before raising event
_previousPowerStatus = powerStatus;
if (Changed != null)
{
Changed(null, EventArgs.Empty);
}
}
}
}
}
但不起作用,因为 PowerStatus 没有任何公共构造函数,我无法存储先前状态的结果...
But doesn't work because PowerStatus hasn't any public constructor and I can't store the result of the previous status...
我该如何管理?
谢谢...
推荐答案
其实有,它叫SystemEvents.PowerModeChanged
如果PowerModeChangedEventArgs
的Mode
为StatusChange
,则表示电池状态发生了变化.
If the PowerModeChangedEventArgs
has a Mode
of StatusChange
, it means the status of the battery has changed.
static void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e)
{
if (e.Mode == Microsoft.Win32.PowerModes.StatusChange)
{
// Check what the status is and act accordingly
}
}
本教程也可能有用:
http://netcode.ru/dotnet/?lang=&katID=30&skatID=277&artID=7643
这篇关于C# - 在 PowerStatus 更改时引发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# - 在 PowerStatus 更改时引发事件
基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01