I am making a Power Managment Monitor for my notebook and I want the program only to show every change in the power status. So, I made a while(true) loop but (of course) it shows even the non changed status. If someone could tell me how to make the loop showing only the change in the power status I'll appreciate it very much!
I hope you've understood my problem!
Well, I tried to compare the old and the new value but it isn't happening. Here's the code:
using System;
using System.Windows.Forms;
using System.Threading;
namespace SimplePMMonitor
{
class PMMonitor
{
static void Main(string[] args)
{
Console.WriteLine("Simple Battery Monitor");
Console.WriteLine("<C> Copyright 2001 Circuit Design Corporation");
Console.WriteLine("<C> Copyright 2011 Simple Corporation");
Console.WriteLine("\nMonitoring started...\n");
PowerStatus pwr = SystemInformation.PowerStatus;
PowerStatus oldPwrStat = SystemInformation.PowerStatus;
DateTime currentDateTime = new DateTime();
currentDateTime = DateTime.Now;
bool initialStart = true;
while (true)
{
Thread.Sleep(1000);
if (pwr.BatteryLifePercent == oldPwrStat.BatteryLifePercent) continue;
if (!initialStart && Math.Abs((int)(pwr.BatteryLifePercent) - (int)(oldPwrStat.BatteryLifePercent)) > 1)
{
Console.WriteLine("=== Battery life has changed more than one percent ===");
}
initialStart = false;
Console.Write("{0} ", currentDateTime);
float remaining = pwr.BatteryLifePercent;
string开发者_C百科 chargeStatus = pwr.BatteryChargeStatus.ToString();
Console.Write("Battery status: {0}, {1}% ", chargeStatus, remaining * 100);
PowerLineStatus powerSource = pwr.PowerLineStatus;
switch (powerSource)
{
case PowerLineStatus.Offline: Console.WriteLine("Battery"); break;
case PowerLineStatus.Online: Console.WriteLine("AC"); break;
default: Console.WriteLine("Unknown"); break;
}
if (pwr.BatteryLifeRemaining != -1)
{
Console.WriteLine("Battery life remaining {0} ", pwr.BatteryLifeRemaining);
}
if (pwr.BatteryFullLifetime != -1)
{
Console.WriteLine("Batery full life remaining {0} ", pwr.BatteryFullLifetime);
}
}
}
}
}
But when I execute it nothing happens. Here's a pic: http://img830.imageshack.us/i/88197124.jpg/
It stays like that and even if I unplug/plug the charger cable it doesn't change.
P.S: Sorry for the bad code...
I am sure you can subscribe to some system events or listen to some windows messages so actually you don't need a while approach, you can simply have your code running and whenever the power status changes you get a notification and you execute some specific code. Have a look here: http://msdn.microsoft.com/en-us/library/aa373163
How about using a timer that checks for changes every few seconds?
In the timer event you get the new value, compare with the old and then if they differ do whatever you want to happen on a change.
Hmm looks like SystemInformation.PowerStatus
always returns the same instance. So you need to backup the individual properties and can't backup pwr
as a whole.
PowerStatus oldPwrStat = pwr;
while (true)
{
Thread.Sleep(1000);
oldBatteryLife=newBatteryLife;
newBatteryLife=BatteryLifePercent;
...
}
The continue
if the battery life hasn't changed is bad too. If the battery life hasn't changed you don't check any other properties at all.
You also should read each property only once per iteration of the loop. Else it might change during one iteration and you get strange bugs.
First of all, make sure you put a Sleep
in your loop, or you will just accomplish draining the battery (the program will use 100% CPU for checking the battery status).
In order to only show changes, store the old value. Only when the new value is not equal to the old value, show the change.
精彩评论