首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用openhardwaremonitor获取GPU利用率

使用openhardwaremonitor获取GPU利用率
EN

Stack Overflow用户
提问于 2020-06-19 13:19:01
回答 1查看 407关注 0票数 1

我正在尝试使用openhardwaremonitor获取我的图形处理器的当前利用率,我使用SensorType.Load来获取CPU的利用率,但是对于图形处理器,它返回的是内存使用情况。我不知道该怎么做

代码语言:javascript
运行
复制
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 + "%";
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-12-14 09:36:35

这个库有点棘手,但是一旦你对它略知一二,剩下的就会慢慢地出来了。我做了这样的事情...

代码语言:javascript
运行
复制
    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
     }

您还必须使用这个OpenHardwareMonitor类,否则数据不会更新。您可以在同一命名空间或其他类文件中使用它

代码语言:javascript
运行
复制
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) { }
  }
}

我仍在学习C#,但希望这对我有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62463744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档