user3681442
user3681442

Reputation: 297

Why am I getting exception when trying to get each process cpu usage?

This is what i did in the class Core:

public static int GetCpuUsage(string name)
        {
            var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", name);
            cpuCounter.NextValue();
            //System.Threading.Thread.Sleep(1000);
            return (int)cpuCounter.NextValue();
        }

I marked not to use the line: //System.Threading.Thread.Sleep(1000); For some reason if i use this line the whoe method is working very very very slow. Once i removed this line it's working fast but all the processes i see or 0% cpu usage or 100%.

This is how i use it in Form1:

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))
                            {

                                cpuusage = Core.GetCpuUsage(p.MachineName);

                                        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 = false;  
                                            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);
                                        }
                                        rcurIndex++;
                            }
                        }
                        catch ( Exception e)
                        {
                            string t = "error";
                        }
                }       

                // if the previout rowscount > current then remove the other rows
                if (rcurIndex < rcount - 1)
                {
                    for (int i = rcurIndex; i < rcount - 1; i++)
                    {
                        dataGridView1.Rows.RemoveAt(rcurIndex);
                    }
                }
        }

This is the results of the cpu usage:

enter image description here

Upvotes: 2

Views: 479

Answers (1)

markhc
markhc

Reputation: 679

Use

var cpuCounter = new PerformanceCounter("Process", "% Processor Time", name); 

Here's what I use to get the CPU usage of a single process (devenv on this case)

foreach (Process process in Process.GetProcesses().Where(x => x.ProcessName == "devenv")) {
            using (PerformanceCounter pcProcess = new PerformanceCounter("Process", "% Processor Time", process.ProcessName))
            {
                pcProcess.NextValue();
                Thread.Sleep(1000);
                return pcProcess.NextValue();
            }
        }

You can easily change it to retrieve the cpu of every process by removing the Where clause. Keep in mind it'll take forever to finish if there are a lot of processes.

Upvotes: 1

Related Questions