The approach given above turns out to be very slow. This is most likely due to the way the queries are being fetched by the ManagementObject. One is encouraged to use the ManagementObjectSearcher instead.
An explicit query narrows the amount of data returned, and significantly improves performance:
private uint? clockSpeed() {
uint? uSpeed = default;
if (OperatingSystem.IsWindows()) {
#if TestSlowManagementObject
const String sPath = "Win32_Processor.DeviceID='CPU0'";
using var mo = new ManagementObject(sPath);
uSpeed = (uint)mo["CurrentClockSpeed"];
#else
const String sQuery = "select CurrentClockSpeed from Win32_Processor";
using var mos = new ManagementObjectSearcher(sQuery);
foreach (var mbo in mos.Get()) {
var properties = mbo.Properties.Cast<PropertyData>();
var pd = properties.FirstOrDefault(pd =>
OperatingSystem.IsWindows() &&
pd.Name == "CurrentClockSpeed");
if (pd != null) {
uSpeed = (uint)pd.Value;
break;
}
}
#endif
}
return uSpeed;
}