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


using System;
using System.Drawing;
using System.Management;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SystemMonitor
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new MainForm());
        }
    }

    public class MainForm : Form
    {
        private float _cpuLoad = 0;
        private ulong _ramUsed = 0;
        private ulong _ramTotal = 1;

        private readonly Font _font = new Font("Segoe UI", 10);
        private CancellationTokenSource _cts = new CancellationTokenSource();

        public MainForm()
        {
            Text = "System Monitor";
            Width = 400;
            Height = 300;
            DoubleBuffered = true;

            StartMonitoring(_cts.Token);
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            _cts.Cancel();
            base.OnFormClosing(e);
        }

        private async void StartMonitoring(CancellationToken token)
        {
            while (!token.IsCancellationRequested)
            {
                var data = await Task.Run(() => GetSystemInfo());

                _cpuLoad = data.cpu;
                _ramUsed = data.used;
                _ramTotal = data.total;

                Invalidate(); // перерисовка формы

                await Task.Delay(1500, token);
            }
        }

        private (float cpu, ulong used, ulong total) GetSystemInfo()
        {
            float cpu = 0;
            ulong total = 0;
            ulong free = 0;

            // CPU
            using (var searcher = new ManagementObjectSearcher("select LoadPercentage from Win32_Processor"))
            {
                foreach (var obj in searcher.Get())
                    cpu = Convert.ToSingle(obj["LoadPercentage"]);
            }

            // RAM
            using (var searcher = new ManagementObjectSearcher(
                "select TotalVisibleMemorySize, FreePhysicalMemory from Win32_OperatingSystem"))
            {
                foreach (var obj in searcher.Get())
                {
                    total = Convert.ToUInt64(obj["TotalVisibleMemorySize"]) / 1024;
                    free = Convert.ToUInt64(obj["FreePhysicalMemory"]) / 1024;
                }
            }

            return (cpu, total - free, total);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;

            int width = ClientSize.Width;
            int height = ClientSize.Height;

            int barWidth = 80;
            int maxBarHeight = 150;

            int centerX = width / 2;

            // CPU столбик
            int cpuHeight = (int)(_cpuLoad / 100 * maxBarHeight);
            Rectangle cpuRect = new Rectangle(centerX - 120, height - 60 - cpuHeight, barWidth, cpuHeight);

            g.FillRectangle(Brushes.Red, cpuRect);
            g.DrawRectangle(Pens.Black, centerX - 120, height - 60 - maxBarHeight, barWidth, maxBarHeight);

            g.DrawString($"CPU {_cpuLoad:F1}%", _font, Brushes.Black,
                centerX - 120, height - 40);

            // RAM столбик
            float ramPercent = (float)_ramUsed / _ramTotal;
            int ramHeight = (int)(ramPercent * maxBarHeight);

            Rectangle ramRect = new Rectangle(centerX + 40, height - 60 - ramHeight, barWidth, ramHeight);

            g.FillRectangle(Brushes.Blue, ramRect);
            g.DrawRectangle(Pens.Black, centerX + 40, height - 60 - maxBarHeight, barWidth, maxBarHeight);

            g.DrawString(
                $"RAM {_ramUsed}/{_ramTotal} MB",
                _font,
                Brushes.Black,
                centerX + 40,
                height - 40
            );
        }
    }
}