【发布时间】:2012-10-12 12:29:17
【问题描述】:
如何获取 CPU 中的逻辑核心数?
我需要这个来确定我应该在我的应用程序中运行多少线程。
【问题讨论】:
-
它依赖于操作系统,那么什么操作系统?
-
任何支持 .net 3.5 的 Windows
标签: c# multithreading .net-3.5
如何获取 CPU 中的逻辑核心数?
我需要这个来确定我应该在我的应用程序中运行多少线程。
【问题讨论】:
标签: c# multithreading .net-3.5
您可以通过 Environment 类获取逻辑处理器的数量
核心数:
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
【讨论】:
使用Environment.ProcessorCount property,它返回逻辑核心数。
【讨论】: