1

I've been trying to create a taskbar tray icon that displays the CPU usage (pulled from wbemtest if possible) when hovered over or clicked on using C#. I used the PercentProcessorTime Name from the ManagementClass Win32_PerfFormattedData_Counters_ProcessorInformation to pull the data. I haven't been able to find what data type the Name is even meant to return. Is there somewhere else I may be able to get the data from?

public void CPUactivitythread()
    {
        //Create a management object to open wbemtest
        ManagementClass CPUdataclass = new ManagementClass("Win32_PerfFormattedData_Counters_ProcessorInformation");

        try
        {
            //While Loop to pull consistent data from the CPU
            while (true)
            {
                //Connect to the CPU Performance Instances in wbemtest
                ManagementObjectCollection CPUobjectCollection = CPUdataclass.GetInstances();

                foreach (ManagementObject obj in CPUobjectCollection) {
                    //Check that the "PercentProcessorTime" instance is there
                    if (obj["Name"].ToString() == "PercentProcessorTime")
                    {
                        if (Convert.ToUInt64(obj["PercentProcessorTime"]) > 0)
                        {
                            cPUUsageToolStripMenuItem.Text = (obj["PercentProcessorTime"]).ToString();
                            CPUoutputLabel.Text = (obj["PercentProcessorTime"]).ToString();
                        }
                        else
                        {

                        }
                    }
                }
                Thread.Sleep(1000);
            }
        }
tmwoods
  • 2,353
  • 7
  • 28
  • 55
Zach R
  • 181
  • 3
  • 15
  • 1
    If all you need is the CPU usage, it's much easier to use performance counters: https://stackoverflow.com/questions/4679962/what-is-the-correct-performance-counter-to-get-cpu-and-memory-usage-of-a-process – Kevin Gosse Jan 24 '18 at 21:46
  • This functionality is present in the built-in Task Manager program. No need to write your own unless you just really want to. – Sam Axe Jan 24 '18 at 22:34
  • @SamAxe I'm aware of task manager, this is just for practice using c#, as well as familiarizing myself with the various tools windows and the .NET framework have to offer. – Zach R Jan 24 '18 at 22:38
  • Do you mean units? Because Google says `uint64` is the data type? – NetMage Jan 24 '18 at 22:57

1 Answers1

1

The objects in the Collection correspond to the Task Manager CPU Information, one for each CPU, one for Total named "_Total". The "PercentProcessorTime" is a property of each performance object. Since you are getting the Formatted data, it has already been calculated ("cooked") according to its performance formula and can be used directly. LINQPad is a really useful tool for exploring objects if you don't like to read documentation :)

Try this:

ManagementClass CPUdataclass = new ManagementClass("Win32_PerfFormattedData_Counters_ProcessorInformation");

try {
    //While Loop to pull consistent data from the CPU
    while (true) {
        //Connect to the CPU Performance Instances in wbemtest
        ManagementObjectCollection CPUobjectCollection = CPUdataclass.GetInstances();

        foreach (ManagementObject obj in CPUobjectCollection) {
            //Check that the "PercentProcessorTime" instance is there
            if (obj["Name"].ToString() == "_Total") {
                var PPT = Convert.ToUInt64(obj.GetPropertyValue("PercentProcessorTime"));
                if (PPT > 0) {
                    cPUUsageToolStripMenuItem.Text = PPT.ToString();
                    CPUoutputLabel.Text = PPT.ToString();
                }
            }
            else {

            }
        }
    }
NetMage
  • 26,163
  • 3
  • 34
  • 55
  • When the program executes the if statement checking if the Name is equal to "_Total", the else statement gets executed instead. – Zach R Jan 25 '18 at 00:49
  • It runs in LINQPad on my Windows 10 PC. Note that the `else` will execute 8 times on my PC for the objects that don't match (e.g. my 8 CPU cores). – NetMage Jan 25 '18 at 01:10
  • I got it working! It was my own stupid mistake. Thanks for the help! :) – Zach R Jan 25 '18 at 01:51