
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to find the Number of CPU Cores in C#?
There are several different pieces of information relating to processors that we can get
- Number of physical processors
- Number of cores
- Number of logical processors
These can all be different; in the case of a machine with 2 dual-core hyper-threadingenabled processors, there are 2 physical processors, 4 cores, and 8 logical processors.
The number of logical processors is available through the Environment class, but the other information is only available through WMI (and you may have to install some hotfixes or service packs to get it on some systems) −
Add a reference in your project to System.Management.dll In .NET Core, this is available (for Windows only) as a NuGet package.
Physical Processors
Example
class Program{ public static void Main(){ foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get()){ Console.WriteLine("Number Of Physical Processors: {0} ", item["NumberOfProcessors"]); } Console.ReadLine(); } }
Output
Number Of Physical Processors: 1
Cores
class Program{ public static void Main(){ 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); Console.ReadLine(); } }
Output
Number Of Cores: 2
Logical Processors
class Program{ public static void Main(){ Console.WriteLine("Number Of Logical Processors: {0}", Environment.ProcessorCount); Console.ReadLine(); } }
Output
Number Of Logical Processors: 4
- Related Articles
- C# program to get the total number of cores on a computer
- How to return the current CPU time in Python?
- What are the components of the CPU?
- How to find the sum of digits of a number using recursion in C#?
- C/C++ Program to Find the Number Occurring Odd Number of Times?
- Program execution in CPU
- Find the number of boxes to be removed in C++
- Find the number of solutions to the given equation in C++
- How to find the unique combination of sum from the given number C#?
- How to find a number in a string in C#?
- Program to find the Hidden Number in C++
- Find the number of jumps to reach X in the number line from zero in C++
- How to retrieve the Azure VM RAM and CPU size using PowerShell?
- How do you find the number of dimensions of an array in C#?
- How to find the power of any given number by backtracking using C#?

Advertisements