I have a method to determine battery level and kept a timer to periodically check the battery level.
void GetBatteryLevel()
{
try
{
//Use this code in next build
//System.Windows.Forms.PowerStatus status = System.Windows.Forms.SystemInformation.PowerStatus;
double batteryLevel;
ManagementObjectSearcher mos = new ManagementObjectSearcher("select EstimatedChargeRemaining from Win32_Battery");
ManagementObjectCollection collection = mos.Get();
foreach (ManagementObject mo in collection)
{
UInt16 remainingPercent = (UInt16)mo["EstimatedChargeRemaining"];
batteryLevel = (double)remainingPercent;
batteryLevel = batteryLevel / 100;
batteryLevel = 1.0 - batteryLevel;
BatteryLevel = new System.Windows.Point(0.0, batteryLevel);
}
}
catch (Exception exp)
{
Logger.LogMessage("EXCEPTION: " + exp.StackTrace);
}
}
Is there a way to register for events when battery level drops or increases by 1%? I have already registered for SystemEvents.PowerModeChanged and that works fine.
batteryLevel
is different thanlastBatteryLevel
, like I said, there may be better ways. – Puli