我一直在尝试创建一个任务栏托盘图标,wbemtest
当将鼠标悬停在或单击 using 时显示 CPU 使用率(如果可能的话)C#
。我使用了PercentProcessorTime
NameManagementClass
Win32_PerfFormattedData_Counters_ProcessorInformation
来提取数据。我无法找到 Name 甚至要返回的数据类型。还有其他地方我可以从中获取数据吗?
public void CPUactivitythread()
{
//Create a management object to open wbemtest
ManagementClass CPUdataclass = new ManagementClass("Win32_PerfFormattedData_Counters_ProcessorInformation");
try
{
//While Loop to pull consistent data from the CPU
while (true)
{
//Connect to the CPU Performance Instances in wbemtest
ManagementObjectCollection CPUobjectCollection = CPUdataclass.GetInstances();
foreach (ManagementObject obj in CPUobjectCollection) {
//Check that the "PercentProcessorTime" instance is there
if (obj["Name"].ToString() == "PercentProcessorTime")
{
if (Convert.ToUInt64(obj["PercentProcessorTime"]) > 0)
{
cPUUsageToolStripMenuItem.Text = (obj["PercentProcessorTime"]).ToString();
CPUoutputLabel.Text = (obj["PercentProcessorTime"]).ToString();
}
else
{
}
}
}
Thread.Sleep(1000);
}
}