Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can i get the cpu's currentClockSpeed, minClockSpeed, maxClockSped.
I have use wmi to do this,but i think the result is not good.
There are no minClockSpeed,and the currentClockSpeed and maxClockSped have the same value,
The currentClockSpeed never changed.My pc's cpu is I5-2300.it support the turbo boost.
Any help will be appreciated.
Posted
Updated 8-Mar-23 15:52pm

You can try using this..
C#
using System;
using System.Management;
using Microsoft.Win32;
namespace ConsoleApplication1
{
        class Program
    {
        
        static void Main(string[] args)
        {
            CPUSpeed();
        }
     
        public static void CPUSpeed()
        {
            uint CurrentClockSpeed, MaxClockSpeed;
            using (ManagementObject Mobj = new ManagementObject("Win32_Processor.DeviceID='CPU0'"))
            {
                CurrentClockSpeed = (uint)(Mobj["CurrentClockSpeed"]);
                MaxClockSpeed = (uint)(Mobj["MaxClockSpeed"]);


                Console.WriteLine("CurrentClockSpeed: " + CurrentClockSpeed);
                Console.WriteLine("MaxClockSpeed: " + MaxClockSpeed);
               
                Console.ReadLine();
            }
        }
    }
    }
 
Share this answer
 
v3
The approach given above turns out to be very slow. This is most likely due to the way the queries are being fetched by the ManagementObject. One is encouraged to use the ManagementObjectSearcher instead.

An explicit query narrows the amount of data returned, and significantly improves performance:

private uint? clockSpeed() {
  uint? uSpeed = default;
  if (OperatingSystem.IsWindows()) {    // Suppress SupportedOSPlatform warnings
#if TestSlowManagementObject
    const String sPath = "Win32_Processor.DeviceID='CPU0'";
    using var mo = new ManagementObject(sPath);
    uSpeed = (uint)mo["CurrentClockSpeed"];
#else
    //[Note]Specifying the CurrentClockSpeed column improves performance
    const String sQuery = "select CurrentClockSpeed from Win32_Processor";
    using var mos = new ManagementObjectSearcher(sQuery);
    foreach (var mbo in mos.Get()) {
      var properties = mbo.Properties.Cast<PropertyData>();
      var pd = properties.FirstOrDefault(pd =>
        OperatingSystem.IsWindows() &&
        pd.Name == "CurrentClockSpeed");

      if (pd != null) {
        uSpeed = (uint)pd.Value;
        break;
      }
    }
#endif
  }
  return uSpeed;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900