There are two special issues that you should consider when you create counters and categories. First, you cannot create custom categories and counters on remote computers. Second, your interaction with custom counters and categories is restricted to read-only mode unless you explicitly specify otherwise. In read-only mode, you cannot increment or decrement them or set the raw value or other values within them. You can use the ReadOnly property to set a custom counter to writable mode.

It is important to note the difference between creating a counter and creating an instance of the PerformanceCounter component. When you create a counter, you are creating a new category and its associated counters in the Windows operating system, rather than a component in your project or application. When you create an instance of the PerformanceCounter component, you create a component inside your Visual Studio project that references an external counter.

When you create a performance counter, realize that the resource may already exist. Another process, perhaps a malicious one, may have already created the resource and have access to it. When you put data in the performance counter, the data is available to the other process.

Note:

The PerformanceCounter class is not fully supported on Microsoft Windows NT version 4.0. You can read from the system counters, but you cannot create, write to, or delete custom counters.

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 a new category and custom performance counter at design time

Open Server Explorer and expand the node for the server you want to view.

Right-click the Performance Counters node and select Create New Category.

The Performance Counter Builder dialog box appears.

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.

Enter a name and description for the category you want to create.

Note:

If you specify the name of an existing category, an error will be raised. To overwrite an existing counter category, you must first delete the category by using the Delete method, and then add a new category.

In the Counter List Builder frame, do the following:

Click the New button.

In the Counter frame, specify a name for the counter you want to create in the category.

Choose a type from the Type drop-down list.

Enter a description for the counter.

Repeat step 4 for each counter you want to create in this category.

Tip:

Before you exit the dialog box, you can select any one of the counters in the Counters list and edit their values, or delete the counters.

Note:

By default, counters and categories you create in the dialog box are read-write enabled, but your interaction with them through an instance of the PerformanceCounter component will be restricted to read-only unless you specify otherwise.

To create a new category and set of performance counters programmatically