diff --git a/dotnet-platform-specific/RetrievalOfCpuCores/RetrievalOfCpuCores/CpuInformation.cs b/dotnet-platform-specific/RetrievalOfCpuCores/RetrievalOfCpuCores/CpuInformation.cs
index 7bd098e0ed..5af134792f 100644
--- a/dotnet-platform-specific/RetrievalOfCpuCores/RetrievalOfCpuCores/CpuInformation.cs
+++ b/dotnet-platform-specific/RetrievalOfCpuCores/RetrievalOfCpuCores/CpuInformation.cs
@@ -1,4 +1,5 @@
-using System.Runtime.InteropServices;
+using System.Management;
+using System.Runtime.InteropServices;
namespace RetrievalOfCpuCores;
@@ -7,16 +8,17 @@ public class CpuInformation
const string wmiQuery = "Select * from Win32_ComputerSystem";
///
- /// Retrieves the number of logical processors of the CPU by using Environment.ProcessorCount.
+ /// Retrieves the number of processors available to the current process by using Environment.ProcessorCount.
///
- /// number of logical processors
+ /// number of processors available to the process
public int GetProcessorsCount()
{
return Environment.ProcessorCount;
}
///
- /// Retrieves the number of logical processors of the CPU.
+ /// Retrieves the number of logical processors of the machine.
+ /// This is only supported on Windows.
///
/// number of logical processors
public int GetLogicalProcessors()
@@ -26,9 +28,10 @@ public int GetLogicalProcessors()
throw new PlatformNotSupportedException();
}
- foreach (var item in new System.Management.ManagementObjectSearcher(wmiQuery).Get())
+ using var searcher = new ManagementObjectSearcher(wmiQuery);
+ foreach (var item in searcher.Get())
{
- return int.Parse(item["NumberOfLogicalProcessors"].ToString());
+ return Convert.ToInt32(item["NumberOfLogicalProcessors"]);
}
return 0;
@@ -36,7 +39,7 @@ public int GetLogicalProcessors()
///
/// Retrieves the number of cores of the CPU.
- /// This is only supported on windows.
+ /// This is only supported on Windows.
///
/// number of cores of the CPU
public int GetNumberOfCores()
@@ -47,10 +50,10 @@ public int GetNumberOfCores()
}
var coreCount = 0;
- const string wmiQuery = "Select * from Win32_Processor";
- foreach (var item in new System.Management.ManagementObjectSearcher(wmiQuery).Get())
+ using var searcher = new ManagementObjectSearcher("Select * from Win32_Processor");
+ foreach (var item in searcher.Get())
{
- coreCount += int.Parse(item["NumberOfCores"].ToString());
+ coreCount += Convert.ToInt32(item["NumberOfCores"]);
}
return coreCount;
@@ -58,7 +61,7 @@ public int GetNumberOfCores()
///
/// Retrieves the number of physical processors of the CPU.
- /// This is only supported on windows.
+ /// This is only supported on Windows.
///
/// number of physical processors
public int GetPhysicalProcessors()
@@ -68,19 +71,21 @@ public int GetPhysicalProcessors()
throw new PlatformNotSupportedException();
}
- foreach (var item in new System.Management.ManagementObjectSearcher(wmiQuery).Get())
+ using var searcher = new ManagementObjectSearcher(wmiQuery);
+ foreach (var item in searcher.Get())
{
- return int.Parse(item["NumberOfProcessors"].ToString());
+ return Convert.ToInt32(item["NumberOfProcessors"]);
}
return 0;
}
///
- /// Retrieves the number of logical processors of the CPU, including also the excluded processors.
+ /// Retrieves the number of logical processors of the CPU, including the excluded processors.
+ /// This is only supported on Windows.
///
- ///
- public int GetExcludedProcessors()
+ /// number of logical processors, including the excluded ones
+ public int GetLogicalProcessorsIncludingExcluded()
{
if (!OperatingSystem.IsWindows())
{
@@ -114,6 +119,16 @@ public int GetExcludedProcessors()
return deviceCount;
}
+ ///
+ /// Retrieves the number of logical processors that are excluded from Windows.
+ /// This is only supported on Windows.
+ ///
+ /// number of excluded processors
+ public int GetExcludedProcessors()
+ {
+ return GetLogicalProcessorsIncludingExcluded() - GetLogicalProcessors();
+ }
+
[DllImport("setupapi.dll", SetLastError = true)]
private static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid,
[MarshalAs(UnmanagedType.LPStr)] string enumerator,
diff --git a/dotnet-platform-specific/RetrievalOfCpuCores/RetrievalOfCpuCores/Program.cs b/dotnet-platform-specific/RetrievalOfCpuCores/RetrievalOfCpuCores/Program.cs
index d5e0051c01..1dcc6b13bc 100644
--- a/dotnet-platform-specific/RetrievalOfCpuCores/RetrievalOfCpuCores/Program.cs
+++ b/dotnet-platform-specific/RetrievalOfCpuCores/RetrievalOfCpuCores/Program.cs
@@ -2,7 +2,7 @@
CpuInformation cpuInformation = new();
-Console.WriteLine("Processors Count: {0}", cpuInformation.GetProcessorsCount());
+Console.WriteLine("Processors Count: {0}", Environment.ProcessorCount);
Console.WriteLine("Logical Processors: {0}", cpuInformation.GetLogicalProcessors());
@@ -10,4 +10,9 @@
Console.WriteLine("Physical Processors: {0}", cpuInformation.GetPhysicalProcessors());
-Console.WriteLine("Logical Processors (including excluded Processors): {0}", cpuInformation.GetExcludedProcessors());
\ No newline at end of file
+Console.WriteLine("Logical Processors (including excluded Processors): {0}", cpuInformation.GetLogicalProcessorsIncludingExcluded());
+
+Console.WriteLine("Excluded Processors: {0}", cpuInformation.GetExcludedProcessors());
+
+var options = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount };
+Parallel.For(0, 4, options, i => Console.WriteLine("Work item {0} done", i));
diff --git a/dotnet-platform-specific/RetrievalOfCpuCores/RetrievalOfCpuCores/RetrievalOfCpuCores.csproj b/dotnet-platform-specific/RetrievalOfCpuCores/RetrievalOfCpuCores/RetrievalOfCpuCores.csproj
index 220b1c4726..c60e137913 100644
--- a/dotnet-platform-specific/RetrievalOfCpuCores/RetrievalOfCpuCores/RetrievalOfCpuCores.csproj
+++ b/dotnet-platform-specific/RetrievalOfCpuCores/RetrievalOfCpuCores/RetrievalOfCpuCores.csproj
@@ -2,13 +2,13 @@
Exe
- net8.0
+ net11.0
enable
enable
-
+
diff --git a/dotnet-platform-specific/RetrievalOfCpuCores/Tests/RetrievalOfCpuCoresTests.cs b/dotnet-platform-specific/RetrievalOfCpuCores/Tests/RetrievalOfCpuCoresTests.cs
index 9931c306a7..ccc77d6cdd 100644
--- a/dotnet-platform-specific/RetrievalOfCpuCores/Tests/RetrievalOfCpuCoresTests.cs
+++ b/dotnet-platform-specific/RetrievalOfCpuCores/Tests/RetrievalOfCpuCoresTests.cs
@@ -13,7 +13,7 @@ public void WhenGetProcessorCount_ResultIsGreaterThanZero()
}
[TestMethod]
- public void WhenGetLogicalProcessors_LogicalProcessorEqualToEnvironmentProcessorCount()
+ public void WhenGetLogicalProcessors_ResultIsAtLeastEnvironmentProcessorCount_Live()
{
if (!OperatingSystem.IsWindows())
{
@@ -24,11 +24,11 @@ public void WhenGetLogicalProcessors_LogicalProcessorEqualToEnvironmentProcessor
var logicalProcessors = cpuInformation.GetLogicalProcessors();
var processorsCount = cpuInformation.GetProcessorsCount();
- Assert.AreEqual(logicalProcessors, processorsCount);
+ Assert.IsTrue(processorsCount <= logicalProcessors);
}
[TestMethod]
- public void WhenGetLogicalProcessors_ResultIsGreaterThanZero()
+ public void WhenGetLogicalProcessors_ResultIsGreaterThanZero_Live()
{
if (!OperatingSystem.IsWindows())
{
@@ -43,7 +43,7 @@ public void WhenGetLogicalProcessors_ResultIsGreaterThanZero()
[TestMethod]
- public void WhenGetPhysicalProcessors_ResultIsGreaterThanZero()
+ public void WhenGetPhysicalProcessors_ResultIsGreaterThanZero_Live()
{
if (!OperatingSystem.IsWindows())
{
@@ -57,7 +57,7 @@ public void WhenGetPhysicalProcessors_ResultIsGreaterThanZero()
}
[TestMethod]
- public void WhenGetProcessorsCount_ResultIsGreaterThanZero()
+ public void WhenGetProcessorsCount_ResultIsGreaterThanZero_Live()
{
if (!OperatingSystem.IsWindows())
{
@@ -71,7 +71,7 @@ public void WhenGetProcessorsCount_ResultIsGreaterThanZero()
}
[TestMethod]
- public void WhenGetProcessorsCountWithExcludedProcessors_ResultIsGreaterThanZero()
+ public void WhenGetProcessorsCountWithExcludedProcessors_ResultIsGreaterThanZero_Live()
{
if (!OperatingSystem.IsWindows())
{
@@ -79,9 +79,23 @@ public void WhenGetProcessorsCountWithExcludedProcessors_ResultIsGreaterThanZero
}
var cpuInformation = new CpuInformation();
- var processorsCount = cpuInformation.GetExcludedProcessors();
+ var processorsCount = cpuInformation.GetLogicalProcessorsIncludingExcluded();
Assert.IsTrue(processorsCount > 0);
}
+
+ [TestMethod]
+ public void WhenGetExcludedProcessors_ResultIsNotNegative_Live()
+ {
+ if (!OperatingSystem.IsWindows())
+ {
+ return;
+ }
+
+ var cpuInformation = new CpuInformation();
+ var excludedProcessors = cpuInformation.GetExcludedProcessors();
+
+ Assert.IsTrue(excludedProcessors >= 0);
+ }
}
}
\ No newline at end of file
diff --git a/dotnet-platform-specific/RetrievalOfCpuCores/Tests/Tests.csproj b/dotnet-platform-specific/RetrievalOfCpuCores/Tests/Tests.csproj
index cc475c6ef3..a4dbb681c3 100644
--- a/dotnet-platform-specific/RetrievalOfCpuCores/Tests/Tests.csproj
+++ b/dotnet-platform-specific/RetrievalOfCpuCores/Tests/Tests.csproj
@@ -1,7 +1,7 @@
- net8.0
+ net11.0
enable
enable
@@ -10,10 +10,10 @@
-
-
-
-
+
+
+
+