BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

System Information
.NET 1.1+

Obtaining the Number of Logical Processors

There are several ways in which to determine the number of logical processor cores in a computer, including using standard members of the .NET framework or Windows Management Instrumentation queries. This article uses the Environment class.

Environment Class

In the article, "Getting the Processor Speed and Number of Cores", I described a Windows Management Instrumentation (WMI) query that allows you to obtain information about the processors in a computer. This query returned several items, including the number of physical and logical processor cores present, the current and maximum processor clock speeds and the external clock speed. This query is useful when you need all of this information.

When you are only interested in the number of logical processor cores, there is a simpler way to retrieve this value. You can read a property from the Environment class. This is a class that allows you to obtain information about the current execution environment and, in some cases, manipulate that environment. One of the properties of the class is ProcessorCount.

ProcessorCount is a static member that returns an integer containing the number of available logical processors, not physical ones. For example, if the computer contains a single processor with only one core, the value will be 1. For a machine with one quad-core processor, the returned value will be 4. If the machine contains two processors, each with two cores and supporting hyper-threading, the result will be 8.

The code below obtains the number of logical cores in the machine:

int processorCount = Environment.ProcessorCount;
22 December 2013