监控具有相同名称的流程,我做错了什么

时间:2012-10-01 09:25:37

标签: c# process cpu-usage performancecounter

我正在测试WinForm应用程序,尝试监视具有相同名称的进程。但由于某种原因,它无法运作。

但我的表格上的标签保持为零。

表单的全局变量:

Thread checkProcess;
List<PerformanceCounter> pcs;

点击按钮开始监控过程

private void btnMonitor_Click(object sender, EventArgs e)
{
    List<Process> proc = new List<Process>();
    foreach (Process p in Process.GetProcesses())
    {
        if (p.ProcessName.IndexOf(txtProcessName.Text) > -1)
        {
            proc.Add(p);
        }
    }
    pcs = new List<PerformanceCounter>();
    int k = 0;
    foreach (Process p in proc)
    {
        pcs.Add(new PerformanceCounter("Process", "% Processor Time", k > 0 ?
            string.Format("{0}#{1}", p.ProcessName, k) : p.ProcessName, true));
        lstbxPC.Items.Add(k > 0 ?
            string.Format("{0}#{1}", p.ProcessName, k) : p.ProcessName);
        k++;
    }
    checkProcess = new Thread(CheckProcess);
    checkProcess.Start();
}

监视和更新标签的方法(从其自己的线程开始):

private void CheckProcess()
{
    int temp = 0;
    while (true)
    {
        temp = 0;
        foreach (PerformanceCounter pc in pcs)
        {
            if (temp < pc.NextValue())
            {
                temp = (int)pc.NextValue();
            }

        }
        // Set the label in a ThreadSafe way. This does work
        SetControlPropertyThreadSafe(lblCPU, "Text", Convert.ToString(temp) + "%");
        Thread.Sleep(1000);
    }
}

2 个答案:

答案 0 :(得分:1)

有很多事情需要检查:

  • 您的pcs是否有物品? (调试)
  • txtProcessName中的文字是什么? (模式为{process name without exe}#{nb},如devenv#1,devenv#2,...}
  • 你能读性能计数器吗? (检查安全性)
  • 因为您正在将NextValue()强制转换为int,所以您将看不到任何更改&lt; 1%

您还应该更改代码,因为如果进程停止,您将获得异常(计数器类别中缺少实例)。

答案 1 :(得分:1)

尝试使用此尺寸:

    private void btnMonitor_Click(object sender, EventArgs e)
    {
        var proc = Process.GetProcesses().Where(p => p.ProcessName.IndexOf(this.txtProcessName.Text) > -1);
        var k = 1;
        var appendSuffix = proc.Count() > 1;

        this.pcs = new List<PerformanceCounter>();
        foreach (var p in proc)
        {
            this.pcs.Add(new PerformanceCounter(
                "Process",
                "% Processor Time",
                appendSuffix ? string.Format("{0}#{1}", p.ProcessName, k) : p.ProcessName,
                true));
            this.lstbxPC.Items.Add(appendSuffix ? string.Format("{0}#{1}", p.ProcessName, k) : p.ProcessName);
            k++;
        }

        this.checkProcess = new Thread(this.CheckProcess);
        this.checkProcess.Start();
    }

    private void CheckProcess()
    {
        while (true)
        {
            var temp = 0;

            foreach (var pc in this.pcs)
            {
                var nextValue = pc.NextValue();

                if (temp < nextValue)
                {
                    temp = (int)nextValue;
                }
            }

            // Set the label in a ThreadSafe way. This does work.
            SetControlPropertyThreadSafe(lblCPU, "Text", Convert.ToString(temp) + "%");
            Thread.Sleep(1000);
        }
    }