PerformanceCounterCategory 类

表示性能对象,它定义性能计数器的类别。

**命名空间:**System.Diagnostics
**程序集:**System(在 system.dll 中)

语法

声明
Public NotInheritable Class PerformanceCounterCategory
用法
Dim instance As PerformanceCounterCategory
public sealed class PerformanceCounterCategory
public ref class PerformanceCounterCategory sealed
public final class PerformanceCounterCategory
public final class PerformanceCounterCategory

备注

提示

应用于此类的 HostProtectionAttribute 属性 (Attribute) 的 Resources 属性 (Property) 值如下:Synchronization | SharedStateHostProtectionAttribute 不影响桌面应用程序(桌面应用程序一般通过双击图标,键入命令或在浏览器中输入 URL 启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护属性

Note重要事项:

创建或删除性能计数器时需要使用已命名的互斥体来同步基础代码。如果具有高度特权的应用程序锁定该已命名互斥体,则试图创建或删除性能计数器会导致应用程序在锁定被释放之前停止响应。若要帮助避免此问题,请无论何时都不要向不受信任的代码授予 UnmanagedCode 权限。此外,UnmanagedCode 权限可能会允许跳过其他权限,因此应仅向高度信任的代码授予此权限。

PerformanceCounterCategory 实例的 CategoryName 属性显示在“性能查看器”应用程序的“添加计数器”对话框的“性能对象”字段中。

PerformanceCounterCategory 类提供几种用于与计算机上的计数器和类别交互的方法。Create 方法使您能够定义自定义类别。Delete 方法提供从计算机移除类别的方法。GetCategories 方法使您能够查看类别的列表,而 ReadCategory 则检索与单个类别关联的所有计数器和实例数据。

性能计数器发布有关应用程序的性能数据。类别包括物理组件(如处理器、磁盘和内存)和系统对象(如进程和线程)。与同一性能对象相关的系统计数器归入一个指示其共同点的类别。当创建 PerformanceCounter 类的实例时,首先指示该组件将与之交互的类别,然后从该类别中选择一个计数器。

例如,一个 Windows 计数器类别属于“Memory”(内存)类别。此类别内的系统计数器跟踪内存数据,如可用字节数和缓存的字节数。如果要在应用程序中使用缓存的字节,则应创建 PerformanceCounter 组件的实例,将其连接到“Memory”(内存)类别,然后从该类别中选取相应的计数器(在这种情况下,选取“Cached Bytes”(缓存字节))。

虽然系统中有很多可用的计数器类别,但与之交互最频繁的可能是“Cache”(缓存)、“Memory”(内存)、“Objects”(对象)、“PhysicalDisk”(物理磁盘)、“Process”(进程)、“Processor”(处理器)、“Server”(服务器)、“System”(系统)和“Thread”(线程)等类别。

Note重要事项:

PerformanceCounter 类中的 RemoveInstance 方法将释放计数器;如果为该类别选择了重复使用选项,则将重复使用计数器的实例。如果有其他进程(甚至代码的其他部分)在尝试在计数器实例中写入内容,可能会形成争用条件。

示例

下面的代码示例确定 PerformanceCounter 及其 PerformanceCounterCategory 存在于本地计算机上,还是存在于其他计算机上。如果这些对象不存在于本地计算机上,则该示例可以选择创建这些对象。该示例使用 Exists 方法确定 PerformanceCounterCategory 是否存在。如果 PerformanceCounterCategory 不存在且未指定任何计数器名称,或者如果计算机为远程计算机,则该示例退出。

如果提供了 PerformanceCounter 名称,则该示例将使用 CounterExists 方法并向用户显示结果。如果 PerformanceCounter 不存在,则用户可以删除 PerformanceCounterCategory 并使用新的 PerformanceCounter 重新创建。如果用户执行此操作,则将使用 Delete 方法删除该类别。

如果需要,该示例将使用 Create 方法立即创建新的 PerformanceCounterCategoryPerformanceCounter。如果指定了实例名称,则该示例将使用 InstanceExists 方法并显示结果。

Imports System
Imports System.Diagnostics
Imports Microsoft.VisualBasic

Module PerfCounterCatCreateExistMod

    Sub Main(ByVal args() As String)
        Dim categoryName As String = ""
        Dim counterName As String = ""
        Dim instanceName As String = ""
        Dim machineName As String = ""
        Dim categoryHelp As String = ""
        Dim counterHelp As String = ""
        Dim objectExists As Boolean = False
        Dim pcc As PerformanceCounterCategory
        Dim createCategory As Boolean = False

        ' Copy the supplied arguments into the local variables.
        Try
            categoryName = args(0)
            counterName = args(1)
            instanceName = args(2)
            machineName = IIf(args(3) = ".", "", args(3))
            categoryHelp = args(4)
            counterHelp = args(5)
        Catch ex As Exception
            ' Ignore the exception from non-supplied arguments.
        End Try

        ' Verify that the category name is not blank.
        If categoryName.Length = 0 Then
            Console.WriteLine("Category name cannot be blank.")
            Return
        End If

        ' Check whether the specified category exists.
        If machineName.Length = 0 Then
            objectExists = _
                PerformanceCounterCategory.Exists(categoryName)

        Else
            ' Handle the exception that is thrown if the computer 
            ' cannot be found.
            Try
                objectExists = PerformanceCounterCategory.Exists( _
                    categoryName, machineName)
            Catch ex As Exception
                Console.WriteLine("Error checking for existence of " & _
                    "category ""{0}"" on computer ""{1}"":" & vbCrLf & _
                    ex.Message, categoryName, machineName)
                Return
            End Try
        End If

        ' Tell the user whether the specified category exists.
        Console.WriteLine("Category ""{0}"" " & _
            IIf(objectExists, "exists on ", "does not exist on ") & _
            IIf(machineName.Length > 0, _
                "computer ""{1}"".", "this computer."), _
            categoryName, machineName)

        ' If no counter name is given, the program cannot continue.
        If counterName.Length = 0 Then
            Return
        End If

        ' A category can only be created on the local computer.
        If Not objectExists Then
            If machineName.Length > 0 Then
                Return
            Else
                createCategory = True
            End If
        Else
            ' Check whether the specified counter exists.
            If machineName.Length = 0 Then
                objectExists = PerformanceCounterCategory.CounterExists( _
                    counterName, categoryName)
            Else
                objectExists = PerformanceCounterCategory.CounterExists( _
                    counterName, categoryName, machineName)
            End If

            ' Tell the user whether the counter exists.
            Console.WriteLine("Counter ""{0}"" " & _
                IIf(objectExists, "exists", "does not exist") & _
                " in category ""{1}"" on " & _
                IIf(machineName.Length > 0, _
                    "computer ""{2}"".", "this computer."), _
                counterName, categoryName, machineName)

            ' If the counter does not exist, consider creating it.
            If Not objectExists Then

                ' If this is a remote computer, 
                ' exit because the category cannot be created.
                If machineName.Length > 0 Then
                    Return
                Else
                    ' Ask whether the user wants to recreate the category.
                    Console.Write("Do you want to delete and recreate " & _
                        "category ""{0}"" with your new counter? [Y/N]: ", _
                        categoryName)
                    Dim userReply As String = Console.ReadLine()

                    ' If yes, delete the category so it can be recreated later.
                    If userReply.Trim.ToUpper.Chars(0) = "Y" Then
                        PerformanceCounterCategory.Delete(categoryName)
                        createCategory = True
                    Else
                        Return
                    End If
                End If
            End If
        End If

        ' Create the category if it was deleted or it never existed.
        If createCategory Then
            pcc = PerformanceCounterCategory.Create( _
                categoryName, categoryHelp, counterName, counterHelp)

            Console.WriteLine( _
                "Category ""{0}"" with counter ""{1}"" created.", _
                pcc.CategoryName, counterName)

        ElseIf instanceName.Length > 0 Then

            ' If an instance name was given, check whether it exists.
            If machineName.Length = 0 Then
                objectExists = PerformanceCounterCategory.InstanceExists( _
                    instanceName, categoryName)
            Else
                objectExists = PerformanceCounterCategory.InstanceExists( _
                    instanceName, categoryName, machineName)
            End If

            ' Tell the user whether the instance exists.
            Console.WriteLine("Instance ""{0}"" " & _
                IIf(objectExists, "exists", "does not exist") & _
                " in category ""{1}"" on " & _
                IIf(machineName.Length > 0, _
                    "computer ""{2}"".", "this computer."), _
                instanceName, categoryName, machineName)
        End If
    End Sub
End Module

继承层次结构

System.Object
  System.Diagnostics.PerformanceCounterCategory

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

PerformanceCounterCategory 成员
System.Diagnostics 命名空间
PerformanceCounter 类
CounterCreationDataCollection 类
CounterSample 结构