CPU Usage% NotifyIcon Using WMI

397 Views Asked by At

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);
            }
        }
1

There are 1 best solutions below

3
NetMage On BEST ANSWER

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 {

            }
        }
    }