How can I use Performance Counters in C# to monitor 4 processes with the same name?
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.Lengt开发者_如何学Pythonh];
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?
Open perfmon
and look. You need to use names like chrome#2
or, possibly, chrome_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.
精彩评论