Here is a C# code snippet to get the current CPU usage of the system using the PerformanceCounter class from the System.Diagnostics namespace:
main.cs263 chars10 lines
Explanation:
PerformanceCounter
is a class that provides access to Windows performance counters. We create a new instance of the PerformanceCounter
class, specifying the Processor
category, the % Processor Time
counter, and "_Total"
instance (meaning all processors).cpuCounter.NextValue()
twice: the first time to initialize the counter and the second time to get the current usage value. There is a delay of 1 second between these calls to allow the counter to update.gistlibby LogSnag