(C#、WMI)托盘应用程序显示 CPU 温度仅在 SpeedFan 应用程序为 运行 时更新值
(C#, WMI) Tray App Displaying CPU Temp only updates value when SpeedFan App is running
我制作了一个简单的托盘应用程序,它显示 CPU 温度并使用计时器每秒更新一次。
托盘图标显示从 ManagementObjectSearcher 查询中检索到的 "CurrentTemperature" 参数,然后使用该参数创建显示温度值的位图。
它似乎运行良好,除了它总是检索完全相同的温度值。但是,如果我在后台运行 SpeedFan 运行, 温度将相应更新。
谁能告诉我为什么应用程序无法自行检索更新的温度值?
注意:我检查了我机器的 BIOS,没有温度读数
我附上代码供任何感兴趣/愿意提供建议的人使用
class Program
{
private static System.Timers.Timer tmr;
private static ContextMenu CMenu;
private static NotifyIcon trayIcon;
private static Color c;
private static int Temp;
private static int _Temp;
private static void Main(string[] args)
{
tmr = new System.Timers.Timer();
trayIcon = new NotifyIcon();
CMenu = new ContextMenu();
//ContextMenu
CMenu.MenuItems.Add("Exit", Exit_Application);
//trayIcon
trayIcon.ContextMenu = CMenu;
trayIcon.Visible = true;
//Timer
tmr.Interval = 1000;
tmr.Elapsed += new ElapsedEventHandler(onTimerTick);
tmr.Enabled = true;
Temp = 0;
Application.Run();
}
private static void Exit_Application(object Sender, EventArgs e) { trayIcon.Icon = null; Application.Exit(); }
private static void onTimerTick(object Sender, ElapsedEventArgs e)
{
using (ManagementObjectSearcher mos = new ManagementObjectSearcher("root\WMI", "SELECT * FROM MSAcpi_ThermalZoneTemperature"))
{
using (ManagementObject mo = mos.Get().OfType<ManagementObject>().First())
{
_Temp = int.Parse(mo["CurrentTemperature"].ToString()) / 10 - 273; //Convert To Celsius
if (_Temp != Temp)
{
Temp = _Temp;
trayIcon.Icon = CreateIcon(Temp);
}
if (Temp >= 85) { Console.Beep(); }
}
}
}
private static Icon CreateIcon(int value)
{
if (value < 50) { c = Color.FromArgb(0, 255, 0); }
else if (value >= 80){ c = Color.OrangeRed; }
else { c = Color.Yellow; }
using (Bitmap bm = new Bitmap(16, 16))
{
using (Graphics g = Graphics.FromImage(bm))
{
using (Brush b = new SolidBrush(c))
{
g.DrawString(value.ToString(), SystemFonts.DefaultFont, b, 0, 0);
return Icon.FromHandle(bm.GetHicon());
}
}
}
}
}
经过进一步研究,WMI 似乎无法读取 CPU 温度,我找到了解释 Here
我制作了一个简单的托盘应用程序,它显示 CPU 温度并使用计时器每秒更新一次。
托盘图标显示从 ManagementObjectSearcher 查询中检索到的 "CurrentTemperature" 参数,然后使用该参数创建显示温度值的位图。
它似乎运行良好,除了它总是检索完全相同的温度值。但是,如果我在后台运行 SpeedFan 运行, 温度将相应更新。
谁能告诉我为什么应用程序无法自行检索更新的温度值?
注意:我检查了我机器的 BIOS,没有温度读数
我附上代码供任何感兴趣/愿意提供建议的人使用
class Program
{
private static System.Timers.Timer tmr;
private static ContextMenu CMenu;
private static NotifyIcon trayIcon;
private static Color c;
private static int Temp;
private static int _Temp;
private static void Main(string[] args)
{
tmr = new System.Timers.Timer();
trayIcon = new NotifyIcon();
CMenu = new ContextMenu();
//ContextMenu
CMenu.MenuItems.Add("Exit", Exit_Application);
//trayIcon
trayIcon.ContextMenu = CMenu;
trayIcon.Visible = true;
//Timer
tmr.Interval = 1000;
tmr.Elapsed += new ElapsedEventHandler(onTimerTick);
tmr.Enabled = true;
Temp = 0;
Application.Run();
}
private static void Exit_Application(object Sender, EventArgs e) { trayIcon.Icon = null; Application.Exit(); }
private static void onTimerTick(object Sender, ElapsedEventArgs e)
{
using (ManagementObjectSearcher mos = new ManagementObjectSearcher("root\WMI", "SELECT * FROM MSAcpi_ThermalZoneTemperature"))
{
using (ManagementObject mo = mos.Get().OfType<ManagementObject>().First())
{
_Temp = int.Parse(mo["CurrentTemperature"].ToString()) / 10 - 273; //Convert To Celsius
if (_Temp != Temp)
{
Temp = _Temp;
trayIcon.Icon = CreateIcon(Temp);
}
if (Temp >= 85) { Console.Beep(); }
}
}
}
private static Icon CreateIcon(int value)
{
if (value < 50) { c = Color.FromArgb(0, 255, 0); }
else if (value >= 80){ c = Color.OrangeRed; }
else { c = Color.Yellow; }
using (Bitmap bm = new Bitmap(16, 16))
{
using (Graphics g = Graphics.FromImage(bm))
{
using (Brush b = new SolidBrush(c))
{
g.DrawString(value.ToString(), SystemFonts.DefaultFont, b, 0, 0);
return Icon.FromHandle(bm.GetHicon());
}
}
}
}
}
经过进一步研究,WMI 似乎无法读取 CPU 温度,我找到了解释 Here