Loop, which continuos on change; C# -
i making power managment monitor notebook , want program show every change in power status. so, made while(true) loop (of course) shows non changed status. if tell me how make loop showing change in power status i'll appreciate much!
i hope you've understood problem!
well, tried compare old , new value isn't happening. here's 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 1 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); } } } } }
but when execute nothing happens. here's pic: http://img830.imageshack.us/i/88197124.jpg/
it stays , if unplug/plug charger cable doesn't change.
p.s: sorry bad code...
how using timer checks changes every few seconds?
in timer event new value, compare old , if differ whatever want happen on change.
hmm looks systeminformation.powerstatus
returns same instance. need backup individual properties , can't backup pwr
whole.
powerstatus oldpwrstat = pwr; while (true) { thread.sleep(1000); oldbatterylife=newbatterylife; newbatterylife=batterylifepercent; ... }
the continue
if battery life hasn't changed bad too. if battery life hasn't changed don't check other properties @ all.
you should read each property once per iteration of loop. else might change during 1 iteration , strange bugs.
Comments
Post a Comment