Share via


How to: Create PerformanceCounter Component Instances

You create an instance of the PerformanceCounter component when you want to make available performance data about your application or monitor the performance of resources on a system. PerformanceCounter components enable you to connect to existing performance counters, write values to custom counters, and react to values the counters receive.

PerformanceCounter components have no visual user interface. If you add an instance of the PerformanceCounter component to a visual designer (such as the Windows Forms Designer), the component appears in a small area underneath the bottom border of the form. This is known as the component tray and acts as a location to display all the non-visual items associated with the form.

Note

Nonvisual classes that inherit from Component support a visual design surface, similar to the component tray, on which your PerformanceCounter component and other components are arranged. The arrangement of items on this designer is not important, as the interface you see will never be displayed to the application's end users.

There are several ways you can create an instance of the PerformanceCounter component:

  • You can locate the counter you want in Server Explorer and add it to your designer, creating an instance of the PerformanceCounter component that is preconfigured to point to that counter.

  • You can drag an instance of the PerformanceCounter component from the Components tab of the Toolbox to a form or other designer.

  • You can create an instance of the PerformanceCounter component in code.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

To create an instance of the PerformanceCounter components from Server Explorer

  1. In Server Explorer, expand the Servers node, and then expand the node for the server you want to view.

    Note

    If the server you want is not listed, you have to add it. For more information, see How to: Access and Initialize Server Explorer/Database Explorer.

  2. Locate and expand the Performance Counters node underneath it.

    Note

    To access performance counters, you have to be a member of the security group that has access to performance counters (for example, the Performance Monitor Users group). Additionally, you might receive prompts on Windows Vista when you try to perform an action that requires elevated privileges, even when you are running under administrative permissions. For more information, see Windows Vista and Visual Studio.

  3. Locate the counter you want to add to your project, and then right-click it.

  4. Click Add to Designer.

  5. Configure your component. For more information, see How to: Configure PerformanceCounter Component Instances.

To create an instance of the PerformanceCounter component from the Toolbox

  1. Access the Components tab of the Toolbox.

  2. Select the PerformanceCounter icon and drag it to the designer surface for the form or component.

  3. Configure your component. For more information, see How to: Configure PerformanceCounter Component Instances.

To create an instance of the PerformanceCounter component programmatically

  1. Create an instance of the PerformanceCounter class in code. You code might resemble this:

    Dim myCounter As New System.Diagnostics.PerformanceCounter()
    
     System.Diagnostics.PerformanceCounter myCounter =
           new System.Diagnostics.PerformanceCounter();
    
  2. Configure any other necessary properties for your component. For more information, see How to: Configure PerformanceCounter Component Instances.

See Also

Tasks

How to: Configure PerformanceCounter Component Instances

Reference

How to: Access and Initialize Server Explorer/Database Explorer