Retrieving Calculated Performance Counter Values
You retrieve calculated performance counter values by calling the NextValue method on the current counter. The counter's calculated value at the instant that the method executes is calculated and returned to you. Calling NextValue a second time will retrieve a different value, because the calculated value changes as the counter increments.
Note To retrieve more complex views of the data in the counter, you can use samples to retrieve static snapshots of counter data at two points in time, and perform comparisons on those returns. For more information on retrieving a sample, see Retrieving Performance Counter Samples.
To retrieve a counter's calculated value
- Create a PerformanceCounter instance and configure it to interact with the desired category and counter. For more information, see Creating PerformanceCounter Component Instances or Configuring PerformanceCounter Component Instances.
- Call the NextValue method and set the results to an integer variable.
The following example illustrates how to use the NextValue property to retrieve the current value of the Total counter in a custom category called Orders:
' Visual Basic Dim MyCtr As New PerformanceCounter() MyCtr.CategoryName = "Orders" MyCtr.CounterName = "Total" Dim retvalue As Single retvalue = MyCtr.NextValue() // C# System.Diagnostics.PerformanceCounter MyCtr = new System.Diagnostics.PerformanceCounter(); MyCtr.CategoryName= "Orders"; MyCtr.CounterName= "Total"; float retvalue = MyCtr.NextValue();Note The first time you run this code, it will return zero. Subsequent queries for the value will return a figure.