Environment.ProcessorCount 属性

定义

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

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

属性值

Int32

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

示例

以下示例演示了该 ProcessorCount 属性。

// This example demonstrates the 
//     Environment.ProcessorCount property.
using namespace System;
int 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.
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 运行时启动时是固定的。 进程运行时,它不会反映环境设置中的更改。

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

适用于