Partilhar via


How to: Retrieve 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.

NoteNote

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 How to: Retrieve Performance Counter Samples.

To retrieve a counter's calculated value

  1. Create a PerformanceCounter instance and configure it to interact with the desired category and counter. For more information, see How to: Create PerformanceCounter Component Instances or How to: Configure PerformanceCounter Component Instances.

  2. Call the NextValue method and set the results to an Single variable.

    The following example illustrates how to use the NextValue method to retrieve the current value of the Total counter in a custom category called Orders:

    Dim MyCtr As New PerformanceCounter()
    MyCtr.CategoryName = "Orders"
    MyCtr.CounterName = "Total"
    Dim retvalue As Single
    retvalue = MyCtr.NextValue()
    
    System.Diagnostics.PerformanceCounter MyCtr =
        new System.Diagnostics.PerformanceCounter();
    MyCtr.CategoryName = "Orders";
    MyCtr.CounterName = "Total";
    float retvalue = MyCtr.NextValue();
    
    NoteNote

    The first time you run this code, it will return zero. Subsequent queries for the value will return a figure.

See Also

Tasks

How to: Retrieve Performance Counter Samples
How to: Create PerformanceCounter Component Instances
How to: Configure PerformanceCounter Component Instances

Concepts

Performance Counter Value Retrieval