Click here to Skip to main content
14,486,704 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have come a long way since my last time on here, and was able to get GPU loads and temperatures to display with my program. My current issue is oddly getting CPU temperatures from OpenHardwareMonitor.dll which I thought would be on the easier side of things. Under //CPU Temp// in my code you will find what I have written as my function to source CPU temperatures which currently returns no values to my label when I debug. I have requested highestAvailable access so that shouldn't be the issue. I am just lost on what I could be doing wrong if anybody wouldn't mind lending a hand.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Management;
using OpenHardwareMonitor;
using OpenHardwareMonitor.Hardware;
using OpenHardwareMonitor.Collections;

namespace HardwareMonitor1
{
    public partial class Form1 : MetroFramework.Forms.MetroForm
    {
        private const string Format = "{0:0.00}%";

        public Form1()
        {
            InitializeComponent();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            //PerfCounters//
            float fcpu = pCPU.NextValue();
            float fram = pRAM.NextValue();
            float fdisk = pDisk.NextValue() / 1000000;
           
            //ProgrgressBars
            circularProgressBarCPU.Value = (int)fcpu;
            circularProgressBarRAM.Value = (int)fram;
            metroProgressBarDisk.Value = (int)fdisk;
          
            //Labels
            lblCPU.Text = string.Format("{0:0.00}%", fcpu);
            lblRAM.Text = string.Format("{0:0.00}%", fram);
            lblDisk.Text = string.Format("{0:0.00}MB/s", fdisk);
           
            //CPU Name//
            SelectQuery Sq = new SelectQuery("Win32_Processor");
           ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher(Sq);
           ManagementObjectCollection osDetailsCollection = objOSDetails.Get();
           StringBuilder sb = new StringBuilder();
           foreach (ManagementObject mo in osDetailsCollection)
           {
               sb.AppendLine(string.Format("{0}", (string)mo["Name"]));
           }
           metroLabel5.Text = (sb.ToString());

            //GPU Name//

            ObjectQuery wmi = new ObjectQuery("SELECT * FROM Win32_VideoController");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmi);
            ManagementObjectCollection results = searcher.Get();
            StringBuilder gp = new StringBuilder();
            foreach (ManagementObject result in results)
            {
                gp.AppendLine(string.Format("{0}", (string)result["Name"]));
            }
            metroLabel6.Text = (gp.ToString());

            

            Computer c = new Computer()
            {
                GPUEnabled = true,
                CPUEnabled = true
            };
            c.Open();

            //GPU Temp//
            foreach (var hardware in c.Hardware)
            {
                if (hardware.HardwareType == HardwareType.GpuAti)
                {
                    hardware.Update();
                    foreach (var sensor in hardware.Sensors)
                        if (sensor.SensorType == SensorType.Temperature)
                        {
                            double t = (sensor.Value.GetValueOrDefault() * 1.8) + 32;
                            metroLabel7.Text = string.Format("{0:0.0}F", t);
                        }
                }

                
            }
            

            //GPU Load//
            foreach (var hardware in c.Hardware)
            {
                if (hardware.HardwareType == HardwareType.GpuAti) 
                {
                    hardware.Update(); 
                    foreach (var sensor in hardware.Sensors)
                        if (sensor.SensorType == SensorType.Load)
                        {
                            float v = sensor.Value.GetValueOrDefault();
                            GPUPB.Value = (int)v;
                            lblGPU.Text = string.Format("{0:0}%", v);
                        }
                   
                        
                }
            }

            //CPU Temp//
            foreach (var hardware in c.Hardware)
            {
                if (hardware.HardwareType == HardwareType.CPU)
                {
                    hardware.Update();
                    hardware.GetReport();
                    foreach (var sensor in hardware.Sensors)
                        if (sensor.SensorType == SensorType.Temperature)
                        {
                            double p = sensor.Value.GetValueOrDefault();
                            metroLabel8.Text = string.Format("{0:0.0}F", p);
                        }
                }


            }
        }




        private void Form1_Load(object sender, EventArgs e)
        {
            timer.Start();
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {

        }
    }
}
Posted
Updated 24-Mar-20 13:48pm
v2
Comments
Richard MacCutchan 5 days ago
   
As suggested in your original question (which you deleted), use your debugger to see exactly what values are being returned. Or, more importantly, whether any errors are generated.
Member 14780089 4 days ago
   
Not to be blunt, but I stated that in my question. It does not show errors, but it does not show any values to metroLabel8.text as it is supposed to.
Richard MacCutchan 4 days ago
   
So did you actually use the debugger to step through all those foreach loops to see exactly what items are being captured?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100