C#-在PowerStatus更改时引发事件
我已经创建了一个需要处于安全状态的应用程序,因此我希望在后台跟踪计算机的电源状态。如果电池电量(如果有的话)很低或很关键,我不会允许用户继续使用应用程序并正确退出 首先,我感到惊讶的是,没有这样的事件存在,以检测变化。您需要始终手动检查PowerStatus 因此,我在它周围创建了一个包装,类似这样:C#-在PowerStatus更改时引发事件,c#,event-handling,C#,Event Handling,我已经创建了一个需要处于安全状态的应用程序,因此我希望在后台跟踪计算机的电源状态。如果电池电量(如果有的话)很低或很关键,我不会允许用户继续使用应用程序并正确退出 首先,我感到惊讶的是,没有这样的事件存在,以检测变化。您需要始终手动检查PowerStatus 因此,我在它周围创建了一个包装,类似这样: using System; using System.Windows.Forms; namespace MyApp { internal static class BatteryMana
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);
}
}
}
}
}
使用系统;
使用System.Windows.Forms;
名称空间MyApp
{
内部静态类电池管理
{
//
内部静态事件EventHandler已更改;
//
私有静态布尔启动;
专用静态系统.Threading.Timer\u Timer;
专用静态功率状态_previousPowerStatus;
内部静态无效开始()
{
如果(!\u已启动)
{
_开始=真;
ManageBatteryLevel();
}
}
内部静态空隙停止()
{
如果(_已启动)
{
如果(_timer!=null)
{
_timer.Dispose();
_定时器=空;
}
_开始=错误;
}
}
私有静态void ManageBatteryLevel()
{
_previousPowerStatus=新的PowerStatus();
TimeSpan dueTime=新的TimeSpan(0,0,0);//立即开始
TimeSpan period=新的TimeSpan(0,1,0);//每1分钟运行一次
//设置启动OnBatteryLevel更改方法的每个周期的计时器
_timer=新系统.Threading.timer(OnBatteryLevelChange,null,dueTime,period);
}
私有静态void onBatteryLevel更改(对象状态信息)
{
PowerStatus PowerStatus=新的PowerStatus();
如果(!\u先前的功率状态等于(功率状态))
{
//在引发事件之前,确保电池电量是最新的
_previousPowerStatus=powerStatus;
如果(已更改!=null)
{
已更改(null,EventArgs.Empty);
}
}
}
}
}
但不起作用,因为PowerStatus没有任何公共构造函数,并且我无法存储以前状态的结果
我该怎么办
谢谢…如果您试图获取当前电源状态,您需要调用
系统信息。PowerStatus
而不是新的PowerStatus()
。您说得对,MSDN上的信息根本没有用,您可以在此处找到您的任务所需的信息:
我希望有帮助 实际上有,它叫 如果
powermodechangedventargs
具有Mode
的StatusChange
,则表示电池的状态已更改
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
}
}
本教程也可用于:
以下是一些代码,将返回PowerStatus的所有值
Type t = typeof(System.Windows.Forms.PowerStatus);
PropertyInfo[] pi = t.GetProperties();
for( int i=0; i<pi.Length; i++ )
{
Console.WriteLine("Property Name {0}", pi[i].Name);
Console.WriteLine("Property Value {0}", pi[i].GetValue(SystemInformation.PowerStatus, null));
}
Type t=typeof(System.Windows.Forms.PowerStatus);
PropertyInfo[]pi=t.GetProperties();
对于(int i=0;我会给你一个参考,对于在下一个刻度中存储和比较不是很有用。@亨克:这是我回答问题的技术部分而不是如何解决实际问题;)而看起来@w69rdy提供了最佳答案,OP必须缓存PowerStatus
类返回的值,而不是实例本身。我(错误地)认为PowerStatus
是一个简单的结构,而不是一个实际的类。这就是为什么我要寻找!谢谢:)