How can I use Performance Counters in C# to monitor 4 processes with the same name?

。_饼干妹妹 提交于 2019-12-08 08:12:47

问题


I'm trying to create a performance counter that can monitor the performance time of applications, one of which is Google Chrome. However, I notice that the performance time I get for chrome is unnaturally low - I look under the task-manager to realize my problem that chrome has more than one process running under the exact same name, but each process has a different working set size and thus(what I would believe) different processor times. I tried doing this:

// get all processes running under the same name, and make a performance counter
// for each one.
Process[] toImport = Process.GetProcessesByName("chrome");

        instances = new PerformanceCounter[toImport.Length];

        for (int i = 0; i < instances.Length; i++)
        {

           PerformanceCounter toPopulate = new PerformanceCounter
                ("Process", "% Processor Time",
                    toImport[i].ProcessName,
                   true);

            //Console.WriteLine(toImport[i].ProcessName + "#" + i);

            instances[i] = toPopulate;
        }

But that doesn't seem to work at all - I just monitor the same process several times over. Can anyone tell me of a way to monitor separate processes with the same name?


回答1:


Open perfmon and look. You need to use names like chrome#2 or, possibly, chrome_2.




回答2:


Important is to make sure you are not using '#' in the instance name. It worth reading this http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.instancename.aspx and pay special attention to character mapping - it just cost me few hours.



来源:https://stackoverflow.com/questions/2703166/how-can-i-use-performance-counters-in-c-sharp-to-monitor-4-processes-with-the-sa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!