I have a windows service that has some performance counters in it. I have another application that monitors what the service is doing, including the performance counters that way everything is found and available in one place. However I seem to be having an issue with one of the performance counters of type RateOfCountsPerSecond32. If I look at the counters on Perfmon everything seems fine, this particular counter gives a reasonable value, but in my monitoring application the counter always provides 0 as its NextValue.
Below is the code i'm using to retrieve the counters from the given category:
PerformanceCounterCategory pcc = new PerformanceCounterCategory(comboBox1.SelectedItem.ToString());
string stats = string.Empty;
foreach (var counter in pcc.GetCounters())
{
stats += string.Format("{0}:\t {1} : {2}\r\n\r\n", counter.CounterName, counter.NextValue(), counter.RawValue );
}
lblTps.Text = stats;
This only seems to be an issue with this particular type of Counter.
Can anyone else notice anything wrong with this? (other than that I should be using a StringBuilder)
From MSDN:
If the calculated value of a counter depends on two counter reads, the first read operation returns 0.0.
You have to call NextValue() twice after creating this type of counter because it needs to buffer one sample before computing its first value. Subsequent calls to NextValue() will work as expected.