为什么在尝试获取每个进程的cpu使用时会出现异常?

时间:2014-06-08 20:20:21

标签: c# .net winforms

这就是我在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();
        }

我标记为不使用该行://System.Threading.Thread.Sleep(1000); 出于某种原因,如果我使用这一行,那么whoe方法的工作非常非常慢。 一旦我删除了这一行,它的工作速度很快,但我看到的所有流程或0%cpu使用率或100%。

这是我在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);
                    }
                }
        }

这是cpu使用的结果:

enter image description here

1 个答案:

答案 0 :(得分:1)

使用

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

以下是我用来获取单个进程的CPU使用情况(本例中的devenv)

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

您可以通过删除Where子句轻松更改它以检索每个进程的cpu。请记住,如果有很多流程,它将永远完成。