How to get number of CPU's logical cores/threads in C#?
Asked Answered
P

2

17

How I can get number of logical cores in CPU?

I need this to determine how many threads I should run in my application.

Peckham answered 22/10, 2012 at 16:35 Comment(2)
It's OS-dependent, so what OS?Tinatinamou
Any Windows that supports .net 3.5Peckham
P
21

You can get number of logical processors through the Environment class
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"]);
}


Environment.ProcessorCount

 using System;

 class Sample 
 {
     public static void Main() 
     {
        Console.WriteLine("The number of processors on this computer is {0}.", 
           Environment.ProcessorCount);
     }
 }

go through this link http://msdn.microsoft.com/en-us/library/system.environment.processorcount.aspx

Paramagnet answered 22/10, 2012 at 16:41 Comment(5)
I have no ManagementObjectSearcher object in System.Management. Why?Peckham
@Kamil: check Assembly: System.Management (in System.Management.dll)Paramagnet
@Kamil: check it is in System.ComponentModel.ComponentParamagnet
I tried Environment.ProcessorCount, looks OK, i have 2 cores detected on Core Duo T2500 CPU. I have to try this on quad core xeon with HT.Peckham
I found and added reference to that dll. It also works, but much slower than Environment.ProcessorCount. What is the difference? I know why WMI technique works slower, but can results differ? Will Win32_ComputerSystem work on Windows 64?Peckham
E
35

Use the Environment.ProcessorCount property, it returns the number of logical cores.

Endanger answered 22/10, 2012 at 16:39 Comment(1)
Thanks @MikeT, but i had to accept answer that took author little more time. Your answer is satisfying too.Peckham
P
21

You can get number of logical processors through the Environment class
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"]);
}


Environment.ProcessorCount

 using System;

 class Sample 
 {
     public static void Main() 
     {
        Console.WriteLine("The number of processors on this computer is {0}.", 
           Environment.ProcessorCount);
     }
 }

go through this link http://msdn.microsoft.com/en-us/library/system.environment.processorcount.aspx

Paramagnet answered 22/10, 2012 at 16:41 Comment(5)
I have no ManagementObjectSearcher object in System.Management. Why?Peckham
@Kamil: check Assembly: System.Management (in System.Management.dll)Paramagnet
@Kamil: check it is in System.ComponentModel.ComponentParamagnet
I tried Environment.ProcessorCount, looks OK, i have 2 cores detected on Core Duo T2500 CPU. I have to try this on quad core xeon with HT.Peckham
I found and added reference to that dll. It also works, but much slower than Environment.ProcessorCount. What is the difference? I know why WMI technique works slower, but can results differ? Will Win32_ComputerSystem work on Windows 64?Peckham

© 2022 - 2024 — McMap. All rights reserved.