c# - How can I get my graph updating? -
currently working on project takes cpu temperature
, put cool graph. takes first value given stops. doesn't keep updating. have tried while loops not seem work. current code:
using system; using system.windows.forms; using system.collections.generic; using system.drawing; using system.drawing.drawing2d; using openhardwaremonitor; using openhardwaremonitor.hardware; namespace pc_version { public class circularprogressbar : control { #region "properties" private color _bordercolor; public color bordercolor { { return _bordercolor; } set { _bordercolor = value; this.invalidate(); } } private color _innercolor; public color innercolor { { return _innercolor; } set { _innercolor = value; this.invalidate(); } } private bool _showpercentage; public bool showpercentage { { return _showpercentage; } set { _showpercentage = value; this.invalidate(); } } private int _borderwidth; public int borderwidth { { return _borderwidth; } set { _borderwidth = value; this.invalidate(); } } private float _value; public float value { { return _value; } set { //----------------------------------getting cpu temps-------------------------- computer thiscomputer; thiscomputer = new computer() { cpuenabled = true }; thiscomputer.open(); string temp = ""; list<string> list = new list<string>(); foreach (var hardwareitem in thiscomputer.hardware) { if (hardwareitem.hardwaretype == hardwaretype.cpu) { hardwareitem.update(); foreach (ihardware subhardware in hardwareitem.subhardware) subhardware.update(); //temps foreach (var sensor in hardwareitem.sensors) { if (sensor.sensortype == sensortype.temperature) { temp = sensor.value.tostring(); int chunksize = 2; int stringlength = temp.length; (int = 0; < stringlength; += chunksize) { if (i + chunksize > stringlength) chunksize = stringlength - i; list.add(temp.substring(i, chunksize)); } } } } } // ---------------------------------- giving graph value-------------------- float average = float.parse(list[4]); _value = average; this.invalidate(); } } // -----------------------------drawing graph--------------------------------------------------------------- #endregion #region "constructor" public circularprogressbar() { setstyle(controlstyles.allpaintinginwmpaint, true); setstyle(controlstyles.resizeredraw, true); setstyle(controlstyles.optimizeddoublebuffer, true); _value = 100; _bordercolor = color.orange; _borderwidth = 30; _showpercentage = true; _innercolor = color.darkgray; this.forecolor = color.white; } #endregion #region "painting" protected override void onpaint(painteventargs e) { base.onpaint(e); e.graphics.smoothingmode = smoothingmode.antialias; //measure single parts int diameter = math.min(this.clientsize.width, this.clientsize.height); int innerdiameter = diameter - borderwidth; rectangle pierect = new rectangle(convert.toint32(this.clientsize.width / 2 - diameter / 2), convert.toint32(this.clientsize.height / 2 - diameter / 2), diameter, diameter); rectangle innerrect = new rectangle(convert.toint32(this.clientsize.width / 2 - innerdiameter / 2), convert.toint32(this.clientsize.height / 2 - innerdiameter / 2), innerdiameter, innerdiameter); //draw outer ring using (solidbrush b = new solidbrush(bordercolor)) { e.graphics.fillpie(b, pierect, 0, value / 100 * 360); } //draw inner ring using (solidbrush b = new solidbrush(this._innercolor)) { e.graphics.fillellipse(b, innerrect); } //draw percentage if (showpercentage) { using (stringformat sf = new stringformat()) { sf.alignment = stringalignment.center; sf.linealignment = stringalignment.center; using (solidbrush b = new solidbrush(this.forecolor)) { e.graphics.drawstring(convert.toint32(value).tostring() + "°c", this.font, b, innerrect, sf); } } } this.update(); } } #endregion }
this image of result: http://pasteboard.co/payhmnb.png
how can add existing code keep updating value , value on form? thank you
this straight forward. strip out logic setter on value property , put own function, it's bad practice place logic getters , setters beyond getting , setting private member value. this:
timer polltimer = new system.timers.timer(1000); // fires every second public void initialize() { // subscribe polltemperature function elapsed event. polltimer.elapsed += polltemperature; // enable periodic polling polltimer.enabled = true; } private float _value; public float value { { return _value; } set { if(_value != value) { _value = value } } } public decimal gettemperature() { // temperature of cpu sensor here float yourtemperature = resultsofyourlogic; return yourtemperature; } void polltemperature(object source, elapsedeventargs e) { value = gettemperature(); this.invalidate(); this.update(); }
this sets timer polls temperatures every second , updates value property. invalidates control , updates redraw it.
Comments
Post a Comment