5

I am writing a program that monitors various resources of the computer, such as CPU usage and so on. I want to monitor GPU usage (the GPU load, not temperature) as well.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using DannyGeneral;


namespace CpuUsage
{
    public partial class Form1 : Form
    {
        private bool processEnded;
        private Process[] processList;
        private string timeStarted;
        private string timeEnded;
        private List<float> processValues;
        private bool alreadyRun;
        private DateTime dt;
        private DateTime dt1;
        private PerformanceCounter theCPUCounter;
        private PerformanceCounter theMemCounter;
        private PerformanceCounter specProcessCPUCounter;
        private float cpuUsage;
        private float memUsage;
        private List<float> Values;

        public Form1()
        {
            InitializeComponent();

            processEnded = false;
            processList = Process.GetProcesses();
            for (int i = 0; i < processList.Count(); i++)
            {
                listView1.Items.Add(processList[i].ProcessName);
            }
            processValues = new List<float>();
            alreadyRun = false;
            dt = DateTime.Now;


            Values = new List<float>();
                theCPUCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
                theMemCounter = new PerformanceCounter("Memory", "Available MBytes");
                specProcessCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);

                Logger.Write("The Program Started At ***   " + DateTime.Now.ToShortTimeString());

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            memUsage = theMemCounter.NextValue();
            label1.Text = memUsage.ToString();
            Logger.Write("Memory Usage   " + memUsage.ToString());
            cpuUsage = this.theCPUCounter.NextValue();
            label2.Text = cpuUsage.ToString();
            Logger.Write("Cpu Usage   " + this.cpuUsage.ToString());
            Values.Add(cpuUsage);
            isProcessRunning();
            if (alreadyRun == true)
            {
                processValues.Add(cpuUsage);
            }

        }
Superbest
  • 25,318
  • 14
  • 62
  • 134
Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • 2
    For future questions please consider providing smaller sample with only relevant code. Your current one have way too much UI-related stuff where simple `Console.Write("CPU usage:{0}", new PerformanceCounter("Processor", "% Processor Time", "_Total"); .NextValue())` would suffice. – Alexei Levenkov Sep 01 '12 at 01:56
  • I use the following answer to read the CPU load: https://stackoverflow.com/a/71481615/171846 – Fidel Mar 15 '22 at 11:40

1 Answers1

3

The video card's own CPU is called a GPU.

Look for performance counters related to that or video (using Start->Run->PerfMon or typing PerfMon in the Start menu's search box; right-click on the graph and choose Add Counter...).

If your video card maker didn't provide any performance counters for the GPU, there aren't any for you to get.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Surely performance counters are not the only source of information about a running system. – Ben Voigt Sep 01 '12 at 04:43
  • @BenVoigt: I never meant to say they were. I suggested that as a source, based on the other code posted. Feel free to offer others. :-) – Ken White Sep 01 '12 at 04:49
  • I'm not sure what your final sentence means then. In any case, there's RivaTuner, which comes with an API to access data programmatically, as well as many other monitoring programs which get this data without performance counters. – Ben Voigt Sep 01 '12 at 05:00
  • @BenVoigt I meant "if you look for a performance counter for video or GPU and you don't find one, your video card manuf. didn't provide one". I'm not sure what part of that isn't clear - how can I phrase it more clearly? – Ken White Sep 01 '12 at 05:04
  • What is the Property name / key name to get the GPU usage in .NET? – linquize Sep 01 '12 at 05:42