Skip to content

Commit

Permalink
Fix exception when fetching processor performance
Browse files Browse the repository at this point in the history
On some systems this API seems to fail, so just assume that the processor is at 100 % performance in those cases, rather than failing the entire run.
  • Loading branch information
Frooxius committed Jun 22, 2023
1 parent c9ac97a commit 5133113
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions Hardware.Info/Windows/HardwareInfoRetrieval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,19 @@ public List<CPU> GetCpuList(bool includePercentProcessorTime = true)
: "SELECT Caption, CurrentClockSpeed, Description, L2CacheSize, L3CacheSize, Manufacturer, MaxClockSpeed, Name, NumberOfCores, NumberOfLogicalProcessors, ProcessorId, SocketDesignation FROM Win32_Processor";
using ManagementObjectSearcher mos = new ManagementObjectSearcher(_managementScope, query, _enumerationOptions);

using PerformanceCounter cpuCounter = new PerformanceCounter("Processor Information", "% Processor Performance", "_Total");
float processorPerformance = cpuCounter.NextValue();
System.Threading.Thread.Sleep(1); // the first call to NextValue() always returns 0
processorPerformance = cpuCounter.NextValue();
float processorPerformance = 100f;

try
{
using PerformanceCounter cpuCounter = new PerformanceCounter("Processor Information", "% Processor Performance", "_Total");
processorPerformance = cpuCounter.NextValue();
System.Threading.Thread.Sleep(1); // the first call to NextValue() always returns 0
processorPerformance = cpuCounter.NextValue();
}
catch
{
// Ignore performance counter errors and just assume that it's at 100 %
}

uint L1InstructionCacheSize = 0;
uint L1DataCacheSize = 0;
Expand Down

0 comments on commit 5133113

Please sign in to comment.