0

Question

How do I get the cpu usage of each process into PopulateApplications()?

What's happening

getCPUUsage() gives me the same value for each process. It's like it's getting the cpu usage for only one process.

The rest of the code seems to work fine.

getCPUUsage() from class Core:

public static double  getCPUUsage()
        {
            ManagementObject processor = new ManagementObject("Win32_PerfFormattedData_PerfOS_Processor.Name='_Total'");
            processor.Get();

            return double.Parse(processor.Properties["PercentProcessorTime"].Value.ToString());
        }

What I've tried

In form1, I have a method by which I display information about processes like icons, name, and statuses (i.e. running/not running).

void PopulateApplications()
        {
            DoubleBufferedd(dataGridView1, true);
                int rcount = dataGridView1.Rows.Count;
                int rcurIndex = 0;
                foreach (Process p in Process.GetProcesses())
                {
                        try
                        {
                            if (File.Exists(p.MainModule.FileName))
                            {                                   
                                        var icon = Icon.ExtractAssociatedIcon(p.MainModule.FileName);
                                        Image ima = icon.ToBitmap();
                                        ima = resizeImage(ima, new Size(25, 25));
                                        ima = (Image)(new Bitmap(ima, new Size(25, 25)));
                                        String status = p.Responding ? "Running" : "Not Responding";
                                        if (rcurIndex < rcount - 1)
                                        {
                                            var currentRow = dataGridView1.Rows[rcurIndex];
                                            currentRow.Cells[0].Value = ima;
                                            currentRow.Cells[1].Value = p.ProcessName;
                                            currentRow.Cells[2].Value = cpuusage;
                                            currentRow.Cells[3].Value = status;
                                        }
                                        else
                                        {
                                            dataGridView1.Rows.Add(
                                                ima, p.ProcessName,cpuusage, status);//false, ima, p.ProcessName, status);
                                        }
                                        rcurIndex++;
                            }
                        }
                        catch ( Exception e)
                        {
                            string t = "error";
                        }
                }
                if (rcurIndex < rcount - 1)
                {
                    for (int i = rcurIndex; i < rcount - 1; i++)
                    {
                        dataGridView1.Rows.RemoveAt(rcurIndex);
                    }
                }
        }

I added this line:

currentRow.Cells[2].Value = cpuusage;

cpuusage is double-type variable.

I changed this line, also, to include addition of cpuusage:

dataGridView1.Rows.Add(
                      ima, p.ProcessName,cpuusage, status);

Now I have a background worker event, dowork, whereby I use cpuusage to get the cpu usage values:

this.Invoke(new Action(() => cpuusage = Core.getCPUUsage()));

Maybe I don't need to call the method getCPUUsage() through backgroundworker.

This is what i see when im running the program:

enter image description here

All the processes have the same cpu usage ? Not logic. Then when there is an update i see:

enter image description here

Again all the cells have the same cpu usage value. But on the left there are many processes each should have it's own cpu usage.

user3681442
  • 297
  • 3
  • 15
  • 1
    Does it work as expected when you don't call it through backgroundworker? – Ruslan Jun 08 '14 at 01:13
  • The timing is ok every 5 seconds. The problems are: 1. how to get the cpu usage in getCPUUsage() for every running process ? Now it's getting the cpu usage for only one process im not sure which one. 2. How to add all the values i will get from getCPUUsage() inside the dataGridView1 cells like im doing with the other information in PopulateApplications() ? – user3681442 Jun 08 '14 at 03:47
  • Ruslan yes it does work but not as excepted since its showing one process cpu usage i need to get a List of cpu usage values for all the processes that are runinng now. Lets say there are now 34 processes running i need that getCPUUsage() will return me a List of this values and then i need to add this List with the values to the cells in PopulateApplications(). The same excat im doing now with status ima and processname. Just this 3 are properties of P in the PopulateApplications() so it's easy to use them. – user3681442 Jun 08 '14 at 03:49
  • 3
    I don't see anything in getCPUUsage that is process specific. Am I missing something? I would expect that it wouldn't change given what I see... – BradleyDotNET Jun 08 '14 at 04:33
  • BradleyDotNET i just edited my question added two screenshots displaying what i get and see in the dataGridView look on the column called Cpu . First all the processes have same value of 16 then 29. But each process should show it's own cpu usage. Thats what i get the way im using both methods as they are in my question. – user3681442 Jun 08 '14 at 05:12
  • You should shorten sample code of your question so it is clear what you have problem with. At this point you get CPU usage of single process and show it as CPU usage of all processes - so pictures show "expected" (matching the code) behavior. – Alexei Levenkov Jun 08 '14 at 07:13
  • Bradley is right! Look [here](http://stackoverflow.com/questions/19756454/calculating-process-cpu-usage-from-process-totalprocessortime) for an example of how to get process specific timings. – TaW Jun 08 '14 at 09:59

0 Answers0