类PerformanceCounter文档

时间:2019-03-27 11:39:56

标签: c#

我在使用PerformanceCounter时遇到问题,我想获取CPU温度,但是我只发现了这一点:

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

我还没有找到有关构造器值“ Thermal Zone Information”的文档。在哪里可以找到PerformanceCounter的文档?

1 个答案:

答案 0 :(得分:1)

请参见以下示例如何获取温度计数器的值:

我在性能监视器中添加了用于热区信息的计数器,如下所示: 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