C# · 12月 27, 2021

如何在C#中获取CPU的逻辑内核/线程数?

@H_404_0@如何获得cpu中的逻辑内核数?

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

解决方法 您可以通过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