图书馆有点棘手,但一旦你对它有所了解,其余的就会一点一点地出来。我做了这样的事情......
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#,但希望对您有所帮助