dotnetdev dotnetdev - 1 month ago 11
C# Question

Counter is not single instance, an instance name needs to be specified

In developing a performance monitoring console app, what is passed into the getcategories method?:

PerformanceCounter[] pcArr = pc.GetCounters(string instance);


When using the no param overload, a runtime exception is thrown:

Counter is not single instance, an instance name needs to be specified.

Also, how do you decide between a static utility method or an extension method? For example, I may use a certain utility method quite frequently but then it would be best to add that as an extension method so I don't have to reference another assembly all the time.

Thanks

Answer

You need to specify an instance name in the GetCounters() method. If you look at your perfomance monitor under Control Panel -> Administrative Tools. Look at the 'Instance' column. That's what you need to give to the method. All counters with the same instance name will be returned. If there is nothing in that column '---' then it's a single instance and you don't need to pass anything.

As far as I can tell, the reason for 'instances' is there may be multiple 'devices' to monitor. Under the processer category you could have multiple instances, one for each processor, mine has a dual core, so has a '0', '1', and '_Total'. Memory is single instance (at least on my machine).

I would ask your static utility method or extension method question separately.