PerformanceCounterCategory クラス

定義

パフォーマンス カウンターのカテゴリを定義するパフォーマンス オブジェクトを表します。

public ref class PerformanceCounterCategory sealed
public sealed class PerformanceCounterCategory
type PerformanceCounterCategory = class
Public NotInheritable Class PerformanceCounterCategory
継承
PerformanceCounterCategory

次のコード例では、 と がPerformanceCounterCategoryローカル コンピューター上に存在するか、別のコンピューターに存在するかをPerformanceCounter決定します。 これらのオブジェクトがローカル コンピューターに存在しない場合は、必要に応じて作成します。 メソッドを Exists 使用して、 PerformanceCounterCategory が存在するかどうかを判断します。 PerformanceCounterCategoryが存在せず、カウンター名が指定されていない場合、またはコンピューターがリモート コンピューターの場合、例は終了します。

名前が PerformanceCounter 指定されている場合、この例では メソッドを CounterExists 使用し、結果をユーザーに表示します。 PerformanceCounterが存在しない場合、ユーザーは を削除し、新しい PerformanceCounterを使用して を再作成PerformanceCounterCategoryできます。 ユーザーがそうすると、 メソッドを使用して Delete カテゴリが削除されます。

要求された場合、この例では、 メソッドを使用して 新しい PerformanceCounterCategoryPerformanceCounterCreate 作成します。 インスタンス名を指定した場合、この例では メソッドを InstanceExists 使用して結果を表示します。

using System;
using System.Diagnostics;
using Microsoft.VisualBasic;

class PerfCounterCatCreateExistMod
{

    public static void Main(string[] args)
    {
        string categoryName = "";
        string counterName = "";
        string instanceName = "";
        string machineName = "";
        string categoryHelp = "";
        string counterHelp = "";
        bool objectExists = false;
        PerformanceCounterCategory pcc;
        bool createCategory = false;

        // Copy the supplied arguments into the local variables.
        try
        {
            categoryName = args[0];
            counterName = args[1];
            instanceName = args[2];
            machineName = args[3]=="."? "": args[3];
            categoryHelp = args[4];
            counterHelp = args[5];
        }
        catch(Exception ex)
        {
            // Ignore the exception from non-supplied arguments.
        }

        // Verify that the category name is not blank.
        if (categoryName.Length==0)
        {
            Console.WriteLine("Category name cannot be blank.");
            return;
        }

        // Check whether the specified category exists.
        if (machineName.Length==0)
        {
            objectExists = PerformanceCounterCategory.Exists(categoryName);

        }
        else
        {
            // Handle the exception that is thrown if the computer
            // cannot be found.
            try
            {
                objectExists = PerformanceCounterCategory.Exists(categoryName, machineName);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error checking for existence of " +
                    "category \"{0}\" on computer \"{1}\":"+"\n" +ex.Message, categoryName, machineName);
                return;
            }
        }

        // Tell the user whether the specified category exists.
        Console.WriteLine("Category \"{0}\" "+ (objectExists? "exists on ": "does not exist on ")+
            (machineName.Length>0? "computer \"{1}\".": "this computer."), categoryName, machineName);

        // If no counter name is given, the program cannot continue.
        if (counterName.Length==0)
        {
            return;
        }

        // A category can only be created on the local computer.
        if (!objectExists)
        {
            if (machineName.Length>0)
            {
                return;
            }
            else
            {
                createCategory = true;
            }
        }
        else
        {
            // Check whether the specified counter exists.
            if (machineName.Length==0)
            {
                objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName);
            }
            else
            {
                objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName, machineName);
            }

            // Tell the user whether the counter exists.
            Console.WriteLine("Counter \"{0}\" "+(objectExists? "exists": "does not exist")+
                " in category \"{1}\" on "+(machineName.Length>0? "computer \"{2}\".": "this computer."),
                counterName, categoryName, machineName);

            // If the counter does not exist, consider creating it.
            if (!objectExists)

                // If this is a remote computer,
                // exit because the category cannot be created.
            {
                if (machineName.Length>0)
                {
                    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);
                    string userReply = Console.ReadLine();

                    // If yes, delete the category so it can be recreated later.
                    if (userReply.Trim().ToUpper()=="Y")
                    {
                        PerformanceCounterCategory.Delete(categoryName);
                        createCategory = true;
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }

        // Create the category if it was deleted or it never existed.
        if (createCategory)
        {
            pcc = PerformanceCounterCategory.Create(categoryName, categoryHelp, counterName, counterHelp);

            Console.WriteLine("Category \"{0}\" with counter \"{1}\" created.", pcc.CategoryName, counterName);
        }
        else if(instanceName.Length>0)
        {
            if (machineName.Length==0)
            {
                objectExists = PerformanceCounterCategory.InstanceExists(instanceName, categoryName);
            }
            else
            {
                objectExists = PerformanceCounterCategory.InstanceExists(instanceName, categoryName, machineName);
            }

            // Tell the user whether the instance exists.
            Console.WriteLine("Instance \"{0}\" "+(objectExists? "exists": "does not exist")+
                " in category \"{1}\" on " + (machineName.Length>0? "computer \"{2}\".": "this computer."),
                instanceName, categoryName, machineName);
        }
    }
}
Imports System.Diagnostics

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

注釈

重要

パフォーマンス カウンターを作成または削除するには、名前付きミューテックスを使用して基になるコードを同期する必要があります。 高い特権を持つアプリケーションが名前付きミューテックスをロックした場合、パフォーマンス カウンターを作成または削除しようとすると、ロックが解放されるまでアプリケーションの応答が停止します。 この問題を回避するために、信頼されていないコードにアクセス許可を付与 UnmanagedCode しないでください。 さらに、アクセス許可により、 UnmanagedCode 他のアクセス許可をバイパスできる可能性があり、高信頼コードにのみ付与する必要があります。

PerformanceCounterCategoryインスタンスの プロパティは、パフォーマンス ビューアー アプリケーションの CategoryName [カウンターの追加] ダイアログ ボックスの [パフォーマンス オブジェクト] フィールドに表示されます。

クラスには PerformanceCounterCategory 、コンピューター上のカウンターとカテゴリを操作するためのメソッドがいくつか用意されています。 Createメソッドを使用すると、カスタム カテゴリを定義できます。 メソッドは Delete 、コンピューターからカテゴリを削除する方法を提供します。 GetCategoriesメソッドを使用すると、カテゴリの一覧を表示しながらReadCategory、1 つのカテゴリに関連付けられているすべてのカウンターデータとインスタンス データを取得できます。

パフォーマンス カウンターは、アプリケーションに関するパフォーマンス データを発行します。 カテゴリには、物理コンポーネント (プロセッサ、ディスク、メモリなど) とシステム オブジェクト (プロセスやスレッドなど) が含まれます。 同じパフォーマンス オブジェクトに関連するシステム カウンターは、共通のフォーカスを示すカテゴリにグループ化されます。 クラスの PerformanceCounter インスタンスを作成するときは、最初にコンポーネントが対話するカテゴリを指定してから、そのカテゴリからカウンターを選択します。

たとえば、1 つの Windows カウンター カテゴリはメモリ カテゴリです。 このカテゴリ内のシステム カウンターは、使用可能なバイト数やキャッシュされたバイト数などのメモリ データを追跡します。 アプリケーションにキャッシュされたバイトを操作する場合は、コンポーネントの PerformanceCounter インスタンスを作成し、それを Memory カテゴリに接続し、そのカテゴリから適切なカウンター (この場合は Cached Bytes) を選択します。

システムにより多くのカウンター カテゴリが使用可能になりますが、おそらく最も頻繁にやり取りするカテゴリは、Cache、Memory、Objects、PhysicalDisk、Process、Processor、Server、System、および Thread カテゴリです。

重要

クラスの メソッドは RemoveInstance カウンターを PerformanceCounter 解放し、そのカテゴリに対して再利用オプションが選択されている場合、カウンターのインスタンスが再利用されます。 これにより、別のプロセスまたはコードの別の部分がカウンター インスタンスに書き込もうとしている場合、競合状態が発生する可能性があります。

Note

アプリケーションの実行中ではなく、アプリケーションのインストール中に新しいパフォーマンス カウンター カテゴリを作成することを強くお勧めします。 これにより、オペレーティング システムが登録済みのパフォーマンス カウンター カテゴリの一覧を更新する時間が得られます。 リストが更新されていない場合、カテゴリの使用は失敗します。

Note

.NET Framework 2.0 と共にインストールされたパフォーマンス カウンター カテゴリでは、個別の共有メモリが使用され、各パフォーマンス カウンター カテゴリには独自のメモリがあります。 カテゴリ名\Performance のレジストリ キーに FileMappingSize という名前の DWORD を作成することで、個別の共有メモリのサイズHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<指定できます。> FileMappingSize 値は、カテゴリの共有メモリ サイズに設定されます。 既定のサイズは 10 進数131072です。 FileMappingSize 値が存在しない場合は、 fileMappingSize Machine.config ファイルで指定された要素の属性値 performanceCounters が使用され、構成ファイルの処理に追加のオーバーヘッドが発生します。 レジストリでファイル マッピング のサイズを設定することで、アプリケーションの起動のパフォーマンスを向上させることができます。 ファイル マッピング のサイズの詳細については、「performanceCounters>」を参照してください<

コンストラクター

PerformanceCounterCategory()

PerformanceCounterCategory クラスの新しいインスタンスを初期化し、CategoryName プロパティを空のままにし、MachineName プロパティをローカル コンピューターに設定します。

PerformanceCounterCategory(String)

PerformanceCounterCategory クラスの新しいインスタンスを初期化し、CategoryName プロパティを指定した値に設定し、MachineName プロパティをローカル コンピューターに設定します。

PerformanceCounterCategory(String, String)

PerformanceCounterCategory クラスの新しいインスタンスを初期化し、CategoryName プロパティと MachineName プロパティを指定した値に設定します。

プロパティ

CategoryHelp

カテゴリのヘルプ テキストを取得します。

CategoryName

このカテゴリを定義するパフォーマンス オブジェクト名を取得または設定します。

CategoryType

パフォーマンス カウンター カテゴリ タイプを取得します。

MachineName

このカテゴリが存在するコンピューターの名前を取得または設定します。

メソッド

CounterExists(String)

指定したカウンターがこのカテゴリに登録されているかどうかを判断します。カテゴリは、CategoryName プロパティと MachineName プロパティで指定されます。

CounterExists(String, String)

指定したカウンターが、ローカル コンピューター上の指定したカテゴリに登録されているかどうかを判断します。

CounterExists(String, String, String)

指定したカウンターが、リモート コンピューター上の指定したカテゴリに登録されているかどうかを判断します。

Create(String, String, CounterCreationDataCollection)
古い.
古い.
古い.

指定したカウンターが含まれているカスタム パフォーマンス カウンター カテゴリをローカル コンピューターに登録します。

Create(String, String, PerformanceCounterCategoryType, CounterCreationDataCollection)

指定したカウンターが含まれているカスタム パフォーマンス カウンター カテゴリをローカル コンピューターに登録します。

Create(String, String, PerformanceCounterCategoryType, String, String)

NumberOfItems32 タイプの単一のカウンターが含まれているカスタム パフォーマンス カウンター カテゴリをローカル コンピューターに登録します。

Create(String, String, String, String)
古い.
古い.
古い.

NumberOfItems32 タイプの単一のカウンターが含まれているカスタム パフォーマンス カウンター カテゴリをローカル コンピューターに登録します。

Delete(String)

カテゴリとそれに関連付けられているカウンターをローカル コンピューターから削除します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
Exists(String)

カテゴリがローカル コンピューターに登録されているかどうかを判断します。

Exists(String, String)

指定したコンピューター上にカテゴリが登録されているかどうかを判断します。

GetCategories()

ローカル コンピューターに登録されているパフォーマンス カウンター カテゴリの一覧を取得します。

GetCategories(String)

指定したコンピューターに登録されているパフォーマンス カウンター カテゴリの一覧を取得します。

GetCounters()

インスタンスが 1 つだけ含まれているパフォーマンス カウンター カテゴリのカウンターの一覧を取得します。

GetCounters(String)

インスタンスが 1 つ以上含まれているパフォーマンス カウンター カテゴリのカウンターの一覧を取得します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetInstanceNames()

このカテゴリに関連付けられたパフォーマンス オブジェクト インスタンスの一覧を取得します。

GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
InstanceExists(String)

この PerformanceCounterCategory オブジェクトの CategoryName プロパティで識別されるカテゴリに、指定したパフォーマンス オブジェクト インスタンスがあるかどうかを判断します。

InstanceExists(String, String)

ローカル コンピューター上の指定したカテゴリに、指定したパフォーマンス オブジェクト インスタンスが含まれているかどうかを判断します。

InstanceExists(String, String, String)

指定したコンピューターの指定したカテゴリに、指定したパフォーマンス オブジェクト インスタンスが含まれているかどうかを判断します。

MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ReadCategory()

このパフォーマンス カウンター カテゴリに関連付けられたすべてのカウンターとパフォーマンス オブジェクト インスタンスのデータを読み取ります。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください