Grabbing GPU Utilization using openhardwaremonitor

1.1k Views Asked by At

I'm trying to get the current utilization of my GPU using openhardwaremonitor I've used SensorType.Load to get the utilization of the CPU but for the GPU it is instead returning the memory usage. I'm not sure exactly what to do

if (hardware.HardwareType == HardwareType.GpuNvidia)
{
    hardware.Update();
    foreach (var sensors in hardware.Sensors)
    {
        if (sensors.SensorType == SensorType.Load)
        {
            tester.Text = sensors.Name + ": " + sensors.Value.GetValueOrDefault();
            int gpuLoadInt = Convert.ToInt32(sensors.Value.GetValueOrDefault());
            string gpuLoadString = Convert.ToString(Decimal.Round(gpuLoadInt));
            gpuLoadGuage.Value = gpuLoadInt;
            gpuLoadLabel.Text = gpuLoadString + "%";
        }
    }
}
1

There are 1 best solutions below

1
Nando On

The library is a little tricky but once you know a little about it the rest will come out little by little. I did something like this...

    using OpenHardwareMonitor.Hardware; // Don't forget this
    public partial class MainWindow : Window, IVisitor // Don't forget to include the interface
    {    
      // we need to call the method every X seconds to get data
      readonly System.Windows.Threading.DispatcherTimer dispatcherTimer = new 
      System.Windows.Threading.DispatcherTimer();
    }
    public MainWindow()
    {
      dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
      dispatcherTimer.Interval = new TimeSpan(0, 0, 2); // <= two seconds
    }

    public void VisitComputer(IComputer computer) => computer.Traverse(this);
    public void VisitHardware(IHardware hardware)
    {
     hardware.Update();
     foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
    }
    public void VisitSensor(ISensor sensor) { }
    public void VisitParameter(IParameter parameter) { }
    
    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
      Thread ThreadGpu = new Thread(() => GetGpuInfo());
      ThreadGpu.Start();
    }

    GetGpuInfo()
    {
      Computer computer = new Computer();
      UpdateVisitor updateVisitor = new UpdateVisitor();
       try
       {
        computer.Open(); // You must initialize what you are going to use
        computer.GPUEnabled = true; // You must initialize what you are going to use
        computer.Accept(updateVisitor); // You NEED this line to update

        if (computer.Hardware.Length > 0)
        {
          foreach (var item in computer.Hardware)
          {
            foreach (ISensor gpu in item.Sensors)
            {
              if (gpu.Name == "GPU Memory" && gpu.Index == 1)
                Console.WriteLine("Memory:" + Math.Round((float)gpu.Value) + "Mhz");
              if (gpu.SensorType == SensorType.Temperature)
                Console.WriteLine("Temperature:" + Math.Round((float)gpu.Value) + "°C");
              if (gpu.SensorType == SensorType.Load && gpu.Name == "GPU Core")
                Console.WriteLine("GPU Core:" + gpu.Value);
              if (gpu.SensorType == SensorType.Clock && gpu.Name == "GPU Core")
                Console.WriteLine(gpu.Value + "Mhz");
            }
          }
        }
       }
       catch (Exception ex)
       {
         Console.WriteLine(ex.Message);
       }
       computer.Close(); // Close resources
     }

You must use this OpenHardwareMonitor class as well, otherwise the data is not updated. You can use it in the same namespace or in another classes file

public class UpdateVisitor : IVisitor
  {
    public void VisitComputer(IComputer computer)
    {
      try
      {
        computer.Traverse(this);
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message);
      }
    }

    public void VisitHardware(IHardware hardware)
    {
      try
      {
        hardware.Update();
        foreach (IHardware subHardware in hardware.SubHardware)
          subHardware.Accept(this);
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message);
      }
    }

    public void VisitSensor(ISensor sensor) { }

    public void VisitParameter(IParameter parameter) { }
  }
}

I'm still learning C# but hope this help