共用方式為


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.
'

備註

在 Linux 和 macOS 系統上,適用于所有 .NET 版本,以及從 .NET 6 開始的 Windows 系統上,此 API 會傳回下列專案:

  • 電腦上的邏輯處理器數目。
  • 如果進程是以 CPU 親和性執行,進程所要親和的處理器數目。
  • 如果進程以 CPU 使用率限制執行,CPU 使用率限制會進位到下一個整數。

此 API 傳回的值會在進程存留期的 .NET 執行時間啟動時修正。 在進程執行時,它不會反映環境設定中的變更。

如需處理器群組和邏輯處理器的詳細資訊,請參閱 處理器群組

適用於