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.
Short answer is from the .Net base class library no.
With that said there are power events available but these events reside in kernel32. Microsoft did attempt to give you a hook into some of these events in the SystemEvents class but unfortunately they do not tie into all of them. Looking at the Power Management API there isn't an event that tracks the battery in the way that you desire.
Power Events: http://msdn.microsoft.com/en-us/library/windows/desktop/aa373162(v=vs.85).aspx
Power Management Services has a method called GetSystemPowerStatus that will get you the information you need. If you have an understanding of working with C++ from within .Net you could query the battery information and when it changes fire your own event.
GetSystemPowerStatus: http://msdn.microsoft.com/en-us/library/windows/desktop/aa372693(v=vs.85).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With