0

Currently I am working on a project that takes your CPU temperature and then put it into a cool Graph. But it only takes the first value given then stops. it doesn't keep updating. I have tried while loops but they do not seem to work. This is my 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
    {
        get { return _BorderColor; }
        set
        {
            _BorderColor = value;
            this.Invalidate();
        }
    }

    private Color _InnerColor;
    public Color InnerColor
    {
        get { return _InnerColor; }
        set
        {
            _InnerColor = value;
            this.Invalidate();
        }
    }

    private bool _ShowPercentage;
    public bool ShowPercentage
    {
        get { return _ShowPercentage; }
        set
        {
            _ShowPercentage = value;
            this.Invalidate();
        }
    }

    private int _BorderWidth;
    public int BorderWidth
    {
        get { return _BorderWidth; }
        set
        {
            _BorderWidth = value;
            this.Invalidate();
        }
    }

    private float _Value;
    public float Value
    {
        get { return _Value; }
        set
        {

 //----------------------------------GETTING THE 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;
                                for (int i = 0; i < stringLength; i += chunkSize)
                                {
                                    if (i + chunkSize > stringLength) chunkSize = stringLength - i;
                                    list.Add(temp.Substring(i, chunkSize));

                                }


                            }

                        }

                    }

                }

//  ---------------------------------- giving the graph a value--------------------
                float average = float.Parse(list[4]);


                _Value = average;
                this.Invalidate();

            }
        }

   // -----------------------------drawing the 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 the 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 is an image of the result: http://pasteboard.co/PayHmNB.png

how can i add to the existing code to keep updating the value and the value on the form? Thank you

Neha Shukla
  • 3,572
  • 5
  • 38
  • 69
bob
  • 21
  • 1
  • 1
  • 4

1 Answers1

0

This is fairly straight forward. Strip out the logic from your setter on the Value property and put it into its own function, it's generally bad practice to place logic into getters and setters beyond getting and setting the private member value. so something like this:

Timer pollTimer = new System.Timers.Timer(1000); // fires every second

public void Initialize()
{
   // subscribe the PollTemperature function to the Elapsed event.
   pollTimer.Elapsed += PollTemperature;
   // Enable the periodic polling
   pollTimer.Enabled = true;
}

private float _Value;
public float Value
{
    get { return _Value; }
    set
    { 
       if(_value != value)
       {
          _value = value
       }
    }
 }

 public decimal GetTemperature()
 {
    // get the temperature of the cpu sensor here
    float yourTemperature = resultsOfYourLogic;
    return yourTemperature;
 }

 void PollTemperature(Object source, ElapsedEventArgs e)
 {
    Value = GetTemperature();
    this.Invalidate();
    this.Update();
 } 

This sets up a timer that polls your temperatures every second and updates your Value property. It also Invalidates this control and then Updates it to redraw it.

Daniel Lane
  • 2,575
  • 2
  • 18
  • 33
  • @bob the Invalidate is fine as far as I can see, I could possibly be wrong though, I'm primarily a WPF developer. With that said though you should try to break out all your temperature reading logic and put it into its own class for reusability in your software (and ease of maintenance) – Daniel Lane May 29 '15 at 14:11
  • ( sorry for getting you name wrong last time :p) i have changed everything the value is updating but the form isn't. – bob May 29 '15 at 14:14
  • @bob in my example I call Invalidate and Update, remove those two lines and call Refresh instead and see if that does the trick. – Daniel Lane May 29 '15 at 14:24
  • @bob well, your problem seems to lie in the drawing of the value on your gauge. That is likely a nuance of Winforms. Have a look at http://stackoverflow.com/questions/2376998/force-form-to-redraw and see if that helps you at all. – Daniel Lane May 29 '15 at 14:37
  • @ Daniel i will take a look. yea after adding this code it just takes the that the form has been given as default. – bob May 29 '15 at 14:40
  • Thank you so much for input. getting me one step closer to solving this. – bob May 29 '15 at 14:40
  • by adding the "GetTemperature(); to the _Value in the CircularProgressBar(). it gathers the temperature fine. – bob May 29 '15 at 14:57
  • but doesnt up date still – bob May 29 '15 at 15:07