Environment.ProcessorCount 属性

定义

获取当前进程可用的处理器数。

public:
 static property int ProcessorCount { int get(); };
public static int ProcessorCount { get; }
static member ProcessorCount : int
Public Shared ReadOnly Property ProcessorCount As Integer

属性值

指定可用处理器数的 32 位有符号整数。

示例

下面的示例演示了该 ProcessorCount 属性。

// This example demonstrates the
//     Environment.ProcessorCount property.
using System;

class Sample
{
    public static void Main()
    {
    Console.WriteLine("The number of processors " +
        "on this computer is {0}.",
        Environment.ProcessorCount);
    }
}
/*
This example produces the following results:

The number of processors on this computer is 1.
*/
// This example demonstrates the
//     Environment.ProcessorCount property.
open System

printfn $"The number of processors on this computer is {Environment.ProcessorCount}."

// This example produces the following results:
//     The number of processors on this computer is 1.
' This example demonstrates the 
'     Environment.ProcessorCount property.
Class Sample
   Public Shared Sub Main()
      Console.WriteLine("The number of processors " & _
                        "on this computer is {0}.", _
                        Environment.ProcessorCount)
   End Sub
End Class
'
'This example produces the following results:
'
'The number of processors on this computer is 1.
'

注解

对于所有.NET版本以及从 .NET 6 开始的 Windows 系统上的 Linux 和 macOS 系统上,此 API 将返回以下最低值:

  • 计算机上的逻辑处理器数。
  • 如果进程以 CPU 关联运行,则进程要关联到的处理器数。
  • 如果进程正在运行并具有 CPU 利用率限制,则 CPU 使用率限制向上舍入为下一个整数。

此 API 返回的值固定在进程生存期的.NET运行时启动。 在进程运行时,它不会反映环境设置中的更改。

有关处理器组和逻辑处理器的详细信息,请参阅 处理器组

适用于