有没有办法通过。net / c#找到CPU核数?

PS:这是一个直接的代码问题,不是一个“我应该使用多线程吗?”的问题!: -)


当前回答

我正在寻找同样的事情,但我不想安装任何nuget或服务包,所以我找到了这个解决方案,这是相当简单和直接的, 使用这个讨论,我认为运行WMIC命令并获得该值会很容易,下面是c#代码。你只需要使用系统。管理名称空间(以及用于进程等的更多标准名称空间)。

string fileName = Path.Combine(Environment.SystemDirectory, "wbem", "wmic.exe");
string arguments = @"cpu get NumberOfCores";

Process process = new Process
{
    StartInfo =
    {
        FileName = fileName,
        Arguments = arguments,
        UseShellExecute = false,
        CreateNoWindow = true,
        RedirectStandardOutput = true,
        RedirectStandardError = true
    }
};

process.Start();

StreamReader output = process.StandardOutput;
Console.WriteLine(output.ReadToEnd());


process.WaitForExit();
int exitCode = process.ExitCode;
process.Close();

其他回答

环境。ProcessorCount应该提供本地机器上的核数。

WMI查询很慢,所以尽量只选择所需的成员,而不是使用Select *。

查询时间为3.4s:

foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_Processor").Get())

而这个需要0.122秒:

foreach (var item in new System.Management.ManagementObjectSearcher("Select NumberOfCores from Win32_Processor").Get())

最简单的方法=环境。ProcessorCount 例子来自环境。ProcessorCount财产

using System;

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

我正在寻找同样的事情,但我不想安装任何nuget或服务包,所以我找到了这个解决方案,这是相当简单和直接的, 使用这个讨论,我认为运行WMIC命令并获得该值会很容易,下面是c#代码。你只需要使用系统。管理名称空间(以及用于进程等的更多标准名称空间)。

string fileName = Path.Combine(Environment.SystemDirectory, "wbem", "wmic.exe");
string arguments = @"cpu get NumberOfCores";

Process process = new Process
{
    StartInfo =
    {
        FileName = fileName,
        Arguments = arguments,
        UseShellExecute = false,
        CreateNoWindow = true,
        RedirectStandardOutput = true,
        RedirectStandardError = true
    }
};

process.Start();

StreamReader output = process.StandardOutput;
Console.WriteLine(output.ReadToEnd());


process.WaitForExit();
int exitCode = process.ExitCode;
process.Close();

看看。net是如何在内部做到这一点的,至少可以说是相当有趣的……如下图所示:

namespace System.Threading
{
    using System;
    using System.Runtime.CompilerServices;

    internal static class PlatformHelper
    {
        private const int PROCESSOR_COUNT_REFRESH_INTERVAL_MS = 0x7530;
        private static volatile int s_lastProcessorCountRefreshTicks;
        private static volatile int s_processorCount;

        internal static bool IsSingleProcessor
        {
            get
            {
                return (ProcessorCount == 1);
            }
        }

        internal static int ProcessorCount
        {
            get
            {
                int tickCount = Environment.TickCount;
                int num2 = s_processorCount;
                if ((num2 == 0) || ((tickCount - s_lastProcessorCountRefreshTicks) >= 0x7530))
                {
                    s_processorCount = num2 = Environment.ProcessorCount;
                    s_lastProcessorCountRefreshTicks = tickCount;
                }
                return num2;
            }
        }
    }
}