4

I have a problem using PerformanceCounter, I want to get cpu temperatures but I have only found this:

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

I haven't found documentation for the constructor values ,"Thermal Zone Information". Where can I find documentation for PerformanceCounter?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
surferNet
  • 49
  • 2
  • Have you checked this question https://stackoverflow.com/questions/1195112/how-to-get-cpu-temperature? – Samvel Petrosov Mar 27 '19 at 11:43
  • yes but I want use,if it is possible,PerformanceCounter – surferNet Mar 27 '19 at 11:47
  • Possible duplicate of [C# PerformanceCounter list of possible Parameters?](https://stackoverflow.com/questions/23366831/c-sharp-performancecounter-list-of-possible-parameters) – Sinatr Mar 27 '19 at 12:49

1 Answers1

3

Please see below example how you can obtain values of the Temperature counter:

I have added counter for the Thermal Zone Information in the Performance Monitor like below: enter image description here

And here is my console app, which is getting the value of the counter:

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);
            }
        }
    }
}

As you can see the values of the constructor are correspondingly are :

PerformanceCounter(
    "Thermal Zone Information",    // Object 
    "Temperature",                 // Counter
    @"\_TZ.TZ01")                  // Instance 
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
  • thank you Samuel , If I try using @"\_TZ.TZ01" I get an error ; 'tempCount.RawValue' ha generato un'eccezione di tipo 'System.InvalidOperationException' long {System.InvalidOperationException} , if I try using @"\_TZ.THRM" I get temperature value,if you try using @"\_TZ.THRM get you the temperature? does it (\_TZ.THRM) work on any pc? – surferNet Mar 27 '19 at 12:24
  • @surferNet I am not sure, but I think that the value that you should specify as `Instance` is depending on your local PC, because in my case there are 2 instances "\_TZ.TZ00" and "\_TZ.TZ01" – Samvel Petrosov Mar 27 '19 at 12:30
  • @surferNet Yes, it doesn't. I have updated my answer. In the updated way you don't need to specify instance name and you will get values of the Temperature Counter for all the instances. – Samvel Petrosov Mar 27 '19 at 12:40
  • @surferNet you should also take into consideration that the returned value for the Temperature is in Kelvin. – Samvel Petrosov Mar 27 '19 at 12:46
  • thank you , I think for celsius "value.NextValue() - 273.15f" – surferNet Mar 27 '19 at 14:44
  • @"\_TZ.TZ01" instead of @"\_TZ.THRM" worked for me in my WinForms application. – Neo Sep 14 '21 at 07:58