我正在尝试使用openhardwaremonitor获取GPU的当前利用率 我已经使用SensorType.Load来获取CPU的利用率,但是对于GPU来说,它返回的是内存使用率。我不确定该怎么做
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 + "%";
}
}
}
答案 0 :(得分:0)
该库有些棘手,但是一旦您对此有所了解,其余的库就会一点一点地出现。我做了这样的事...
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类,否则数据不会更新。您可以在相同的名称空间或其他类文件中使用它
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#,但希望能对您有所帮助