Skip to content

Method Environment.ProcessorCount sometimes shows incorrect data #448

@constructor-igor

Description

@constructor-igor
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions