.net 检测处理器数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/188503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting the number of processors
提问by Peter C
How do you detect the number of physical processors/cores in .net?
你如何检测 .net 中物理处理器/内核的数量?
回答by stephbu
System.Environment.ProcessorCount
returns the number of logical processors
返回逻辑处理器的数量
http://msdn.microsoft.com/en-us/library/system.environment.processorcount.aspx
http://msdn.microsoft.com/en-us/library/system.environment.processorcount.aspx
For physical processor count you'd probably need to use WMI - the following metadata is supported in XP/Win2k3 upwards (Functionality enabled in SP's prior to Vista/Win2k8).
对于物理处理器数量,您可能需要使用 WMI - XP/Win2k3 以上支持以下元数据(在 Vista/Win2k8 之前的 SP 中启用功能)。
Win32_ComputerSystem.NumberOfProcessorsreturns physical count
Win32_ComputerSystem.NumberOfLogicalProcessorsreturns logical (duh!)
Win32_ComputerSystem.NumberOfProcessors返回物理计数
Win32_ComputerSystem.NumberOfLogicalProcessors返回逻辑(废话!)
Be cautious that HyperThreaded CPUs appear identical to multicore'd CPU's yet the performance characteristics are verydifferent.
要谨慎的是超线程CPU出现相同multicore'd CPU的但性能特点是非常不同的。
To check for HT-enabled CPUs examine each instance of Win32_Processor and compare these two properties.
要检查启用 HT 的 CPU,请检查 Win32_Processor 的每个实例并比较这两个属性。
Win32_Processor.NumberOfLogicalProcessors
Win32_Processor.NumberOfCores
Win32_Processor.NumberOfLogicalProcessors
Win32_Processor.NumberOfCores
On multicore systems these are typically the same the value.
在多核系统上,这些值通常相同。
Also, be aware of systems that may have multiple Processor Groups, which is often seen on computers with a large number of processors. By default .Net will only using the first processor group- which means that by default, threads will utilize only CPUs from the first processor group, and Environment.ProcessorCountwill return only the number of CPUs in this group. According to Alastair Maw's answer, this behavior can be changed by altering the app.config as follows:
此外,请注意可能有多个处理器组的系统,这在具有大量处理器的计算机上很常见。默认情况下,.Net 将仅使用第一个处理器组- 这意味着默认情况下,线程将仅使用第一个处理器组中的CPU,并且Environment.ProcessorCount将仅返回该组中的 CPU 数量。根据Alastair Maw 的回答,可以通过如下更改 app.config 来更改此行为:
<configuration>
<runtime>
<Thread_UseAllCpuGroups enabled="true"/>
<GCCpuGroup enabled="true"/>
<gcServer enabled="true"/>
</runtime>
</configuration>
回答by Jesse C. Slicer
While Environment.ProcessorCountwill indeed get you the number of virtual processors in the system, that may not be the number of processors available to your process. I whipped up a quick little static class/property to get exactly that:
虽然Environment.ProcessorCount确实会为您提供系统中虚拟处理器的数量,但这可能不是您的进程可用的处理器数量。我快速创建了一个小的静态类/属性来准确地得到它:
using System;
using System.Diagnostics;
/// <summary>
/// Provides a single property which gets the number of processor threads
/// available to the currently executing process.
/// </summary>
internal static class ProcessInfo
{
/// <summary>
/// Gets the number of processors.
/// </summary>
/// <value>The number of processors.</value>
internal static uint NumberOfProcessorThreads
{
get
{
uint processAffinityMask;
using (var currentProcess = Process.GetCurrentProcess())
{
processAffinityMask = (uint)currentProcess.ProcessorAffinity;
}
const uint BitsPerByte = 8;
var loop = BitsPerByte * sizeof(uint);
uint result = 0;
while (--loop > 0)
{
result += processAffinityMask & 1;
processAffinityMask >>= 1;
}
return (result == 0) ? 1 : result;
}
}
}
回答by Rick Minerich
This actually varies quite a bit depending on the target platform. Stephbu's answer will work great on XP SP3 and newer.
这实际上因目标平台而异。Stephbu 的回答在 XP SP3 和更新版本上会很好用。
If you are targeting older platforms, you may want to check out this article. I wrote it about half a year ago and in it I discuss several different ways to do this as well as the individual pros and cons of each method.
如果您的目标是旧平台,您可能需要查看这篇文章。大约半年前我写了它,在其中我讨论了几种不同的方法来做到这一点以及每种方法的利弊。
You may also want to check out this code project articleif you are interested in differentiating shadow cores from hyperthreading from real ones.
如果您有兴趣将影子核心与超线程与真实核心区分开来,您可能还想查看此代码项目文章。
回答by Rob Walker
Environment.ProcessorCount will also include any hyperthreaded processors.
Environment.ProcessorCount 还将包括任何超线程处理器。
There is no way (at least up through Windows 2003) to distinguish a hyperthreaded processor from one with two cores.
没有办法(至少在 Windows 2003 之前)将超线程处理器与具有两个内核的处理器区分开来。
回答by Tsvetomir Tsonev
System.Environment.ProcessorCount is what you need
System.Environment.ProcessorCount 就是你所需要的
回答by liggett78
Environment.ProcessorCount
Environment.ProcessorCount
EDIT: available in .NET 2.0, not in .NET 1.1
编辑:在 .NET 2.0 中可用,在 .NET 1.1 中不可用
回答by aristippus303
Don't have enough rep for the wiki, but note that in addition to XPSP2, Windows 2003 Server SP1 and SP2 also need a hotfix to enable this functionality:
没有足够的 wiki 代表,但请注意,除了 XPSP2,Windows 2003 Server SP1 和 SP2 还需要一个修补程序来启用此功能:
回答by bahrep
You can use PowerShell to access comprehensive processor information. For example, you can run the following command to get the number of CPU cores:
您可以使用 PowerShell 访问全面的处理器信息。例如,您可以运行以下命令来获取 CPU 内核数:
Get-WmiObject -namespace root\CIMV2 -class Win32_Processor -Property NumberOfCores
It's much easier to research WMI when using some kind of explorer tool. So, I can suggest using WMI browsing tool (e.g. WMIExploreror WMI CIM Studio) to explore WMI classes, properties and methods.
使用某种资源管理器工具时,研究 WMI 会容易得多。因此,我建议使用 WMI 浏览工具(例如WMIExplorer或WMI CIM Studio)来探索 WMI 类、属性和方法。

