constructor-igor / TechSugar

Tech. Sugar seminars
7 stars 6 forks source link

Method Environment.ProcessorCount sometimes shows incorrect data #448

Closed constructor-igor closed 4 years ago

constructor-igor commented 4 years ago
using System;

namespace GetProcessorCountCmd
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"Environment.ProcessorCount: {Environment.ProcessorCount}");
            Print();
            Console.WriteLine("Press <Enter> to exit.");
            Console.ReadLine();
        }

        static void Print()
        {
            foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get())
            {
                Console.WriteLine("Number Of Physical Processors: {0} ", item["NumberOfProcessors"]);
            }

            // Number of Cores

            int coreCount = 0;
            foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_Processor").Get())
            {
                coreCount += int.Parse(item["NumberOfCores"].ToString());
            }

            Console.WriteLine("Number Of Cores: {0}", coreCount);

            // Number of Logical Processors

            foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get())
            {
                Console.WriteLine("Number Of Logical Processors: {0}", item["NumberOfLogicalProcessors"]);
            }
        }
    }
}

image

Windows Task Manager: image

https://stackoverflow.com/questions/1542213/how-to-find-the-number-of-cpu-cores-via-net-c