I am designing a program that depends on monitoring the battery level of the computer.
This is the C# code I am using:
PowerStatus pw = SystemInformation.PowerStatus;
if (pw.BatteryLifeRemaining >= 75)
{
//Do stuff here
}
My failed attempt of the while
statement, it uses all the CPU which is undesirable.
int i = 1;
while (i == 1)
{
if (pw.BatteryLifeRemaining >= 75)
{
//Do stuff here
}
}
How do I monitor this constantly with an infinite loop so that when it reaches 75% it will execute some code.
while(true)
instead of your while, second theDo stuff here
stuff can break your code, while(true) won't (of course if you havebreak
somewhere) :) – Fieldwhile (true)
inside the default thread (The GUI thread), the GUI would become iresponsive. To prevent this from happening u need to create another thread (thread, backgroundworker, timer) to handle the while loop. The only problem is that only the GUI thread is able to update the GUI. Thats why u need to invoke the proper GUI thread and let it update its GUI... – Tadeo