Загрузка данных


using System;
using System.Diagnostics;

namespace WindowsFormsApp34
{
    public class CpuMonitor : IDisposable
    {
        private PerformanceCounter cpuCounter;

        public CpuMonitor()
        {
            cpuCounter = new PerformanceCounter(
                "Processor",
                "% Processor Time",
                "_Total"
            );

            cpuCounter.NextValue();
        }

        public int GetCpuLoad()
        {
            try
            {
                float value = cpuCounter.NextValue();

                int result = (int)Math.Round(value);

                if (result < 0)
                    result = 0;

                if (result > 100)
                    result = 100;

                return result;
            }
            catch
            {
                return 0;
            }
        }

        public void Dispose()
        {
            if (cpuCounter != null)
            {
                cpuCounter.Dispose();
                cpuCounter = null;
            }
        }
    }
}