Note that there are some explanatory texts on larger screens.

plurals
  1. POLoop, which continuos on change; C#
    primarykey
    data
    text
    <p>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!</p> <p>I hope you've understood my problem!</p> <p>Well, I tried to compare the old and the new value but it isn't happening. Here's the code:</p> <pre><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("&lt;C&gt; Copyright 2001 Circuit Design Corporation"); Console.WriteLine("&lt;C&gt; 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 &amp;&amp; Math.Abs((int)(pwr.BatteryLifePercent) - (int)(oldPwrStat.BatteryLifePercent)) &gt; 1) { Console.WriteLine("=== Battery life has changed more than one percent ==="); } initialStart = false; Console.Write("{0} ", currentDateTime); float remaining = pwr.BatteryLifePercent; string 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); } } } } } </code></pre> <p>But when I execute it nothing happens. Here's a pic: <a href="http://img830.imageshack.us/i/88197124.jpg/" rel="nofollow">http://img830.imageshack.us/i/88197124.jpg/</a></p> <p>It stays like that and even if I unplug/plug the charger cable it doesn't change.</p> <p>P.S: Sorry for the bad code...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload