10

如何获取 CPU 中的逻辑核心数?

我需要这个来确定我应该在我的应用程序中运行多少线程。

4

2 回答 2

25

使用Environment.ProcessorCount 属性,它返回逻辑核心的数量。

于 2012-10-22T16:39:03.503 回答
18

您可以通过 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);

逻辑处理器的数量

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);
     }
 }

通过此链接http://msdn.microsoft.com/en-us/library/system.environment.processorcount.aspx

于 2012-10-22T16:41:38.697 回答