类 PerformanceCounter 文档

4

我在使用PerformanceCounter时遇到了问题,想要获取CPU温度,但我只找到了以下内容:

PerformanceCounter tempCount = new PerformanceCounter(
    "Thermal Zone Information", 
    "Temperature", 
    @"\_TZ.THRM"); 

我找不到关于构造函数值 "热区信息" 的文档。PerformanceCounter 的文档在哪里可以找到?


你有检查过这个问题吗?https://dev59.com/fXM_5IYBdhLWcg3w3nNs? - Samvel Petrosov
是的,但如果可能的话,我想使用PerformanceCounter。 - surferNet
可能是C# PerformanceCounter 可能参数列表?的重复问题。 - Sinatr
1个回答

3
请参考以下示例,获取 温度计数器 的值:
我在性能监视器中添加了 Thermal Zone 信息的计数器,如下所示: enter image description here 这是我的控制台应用程序,它获取计数器的值:
using System;
using System.Diagnostics;
using System.Threading;

namespace ConsoleApp
{
    public class Program
    {
        public static void Main(params string[] args)
        {
            PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Thermal Zone Information");
            var instances = performanceCounterCategory.GetInstanceNames();
            List<PerformanceCounter> temperatureCounters = new List<PerformanceCounter>();
            foreach (string instanceName in instances)
            {

                foreach (PerformanceCounter counter in performanceCounterCategory.GetCounters(instanceName))
                {
                    if (counter.CounterName == "Temperature")
                    {
                        temperatureCounters.Add(counter);
                    }
                }
            }


            while(true)
            {
                foreach (PerformanceCounter counter in temperatureCounters)
                {
                    Console.WriteLine("{0} {1} {2} {3}",counter.CategoryName,counter.CounterName,counter.InstanceName, counter.NextValue());
                }
                Console.WriteLine();
                Console.WriteLine();
                Thread.Sleep(500);
            }
        }
    }
}

正如您所看到的,构造函数的值相应对应为:

PerformanceCounter(
    "Thermal Zone Information",    // Object 
    "Temperature",                 // Counter
    @"\_TZ.TZ01")                  // Instance 

谢谢Samuel,如果我尝试使用@"_TZ.TZ01",我会收到一个错误;'tempCount.RawValue' ha generato un'eccezione di tipo 'System.InvalidOperationException' long {System.InvalidOperationException},如果我尝试使用@"_TZ.THRM",我会得到温度值,如果你尝试使用@"_TZ.THRM"获取温度,它是否在任何电脑上都可以工作? - surferNet
@surferNet 我不确定,但我认为你应该指定的“Instance”值取决于你的本地PC,因为在我的情况下有两个实例“_TZ.TZ00”和“_TZ.TZ01”。 - Samvel Petrosov
@surferNet 是的,它不需要。我已经更新了我的回答。在更新的方式中,您无需指定实例名称,即可获取所有实例的温度计数器值。 - Samvel Petrosov
@surferNet 你还应该考虑到温度的返回值是以开尔文为单位的。 - Samvel Petrosov
谢谢,我认为对于摄氏度来说,“value.NextValue() - 273.15f”是正确的。 - surferNet
@"_TZ.TZ01" 在我的 WinForms 应用程序中起作用,而不是 @"_TZ.THRM"。 - Neo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接