แก้ไข

แชร์ผ่าน


Environment.ProcessorCount Property

Definition

Gets the number of processors available to the current process.

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

Property Value

The 32-bit signed integer that specifies the number of processors that are available.

Examples

The following example demonstrates the ProcessorCount property.

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

Remarks

On Linux and macOS systems for all .NET versions and on Windows systems starting with .NET 6, this API returns the minimum of:

  • The number of logical processors on the machine.
  • If the process is running with CPU affinity, the number of processors that the process is affinitized to.
  • If the process is running with a CPU utilization limit, the CPU utilization limit rounded up to the next whole number.

The value returned by this API is fixed at .NET runtime startup for the process lifetime. It does not reflect changes in the environment settings while the process is running.

For more information about processor groups and logical processors, see Processor Groups.

Applies to