CPU always shows 0% when installed from Microsoft Store (MSIX sandbox)
Bug Description
When RunCat365 is installed from the Microsoft Store, CPU usage always displays 0%. The cat animation runs at minimum speed and the tooltip shows "CPU: 0.0%".
Root Cause
The app uses PerformanceCounter("Processor", "% Processor Time", "_Total") to read CPU usage. Microsoft Store apps run inside an MSIX sandbox which restricts access to Windows Performance Counters. The PerformanceCounter constructor does not throw an exception, but NextValue() always returns 0.
Proposed Fix
Replace PerformanceCounter with the Win32 GetSystemTimes API via P/Invoke (kernel32.dll), which works correctly inside the MSIX sandbox.
The approach:
- Call
GetSystemTimeswhich returns idle, kernel, and user times asFILETIMEvalues - Calculate CPU usage from the delta between consecutive samples
- Note:
kernelTimefromGetSystemTimesincludes idle time, so actual kernel usage =kernelDelta - idleDelta
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool GetSystemTimes(
out long idleTime,
out long kernelTime,
out long userTime);
This removes the dependency on System.Diagnostics.PerformanceCounter for CPU monitoring while keeping the same CPUInfo struct and averaging logic.
I have tested this fix locally on Windows 11 and confirmed it reports correct CPU usage.
Environment
- Windows 11 (10.0.26100)
- RunCat365 installed from Microsoft Store
- .NET 9.0
@octalparrot Is this issue the same as the existing, resolved issue (https://github.com/Kyome22/RunCat365/issues/293)?
@octalparrot Also, since this issue does not follow the Bug issue template, we will not be addressing it.
is this resolved?
I haven't received a response, so I'm not sure what's going on.
I haven't received a response, so I'm not sure what's going on.
I guess we close ? since no updates since and issue seems to be addressed already.
Is this issue the same as the existing, resolved issue (https://github.com/Kyome22/RunCat365/issues/293)?
I'd say it is the same problem, but he's suggesting a different solution. A solution that requires less action from the user, but on the other hand requires more effort from the developers.
@octalparrot I am closing this issue. Please re-open it if you would like to continue the discussion.
@Kyome22 Should we close the related PR?
@mor39a Yes, we should.