PerformanceCounterCategory 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示效能物件,定義效能計數器的分類。
public ref class PerformanceCounterCategory sealed
public sealed class PerformanceCounterCategory
type PerformanceCounterCategory = class
Public NotInheritable Class PerformanceCounterCategory
- 繼承
-
PerformanceCounterCategory
範例
下列程式代碼範例會 PerformanceCounter 判斷 和 是否存在 PerformanceCounterCategory 於本機計算機或另一部計算機上。 如果這些物件不存在於本機計算機上,則範例會選擇性地加以建立。 它會使用 Exists 方法來判斷 是否存在 PerformanceCounterCategory 。 PerformanceCounterCategory如果 不存在且未指定計數器名稱,或計算機是遠端計算機,則範例會結束。
PerformanceCounter如果提供名稱,此範例會CounterExists使用 方法,並將結果顯示給使用者。 PerformanceCounter如果 不存在,使用者可以使用新的 PerformanceCounter刪除並重新建立 PerformanceCounterCategory 。 如果用戶這麼做,則會使用 Delete 方法刪除類別。
如果已要求,此範例現在會建立新的 PerformanceCounterCategory ,並使用 PerformanceCounterCreate 方法。 如果指定實例名稱,此範例會 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
備註
重要
建立或刪除性能計數器需要使用具名 mutex 同步處理基礎程序代碼。 如果高度特殊許可權的應用程式鎖定具名 Mutex,則嘗試建立或刪除性能計數器會導致應用程式停止回應,直到鎖定釋放為止。 為了協助避免這個問題,請勿將許可權授 UnmanagedCode 與不受信任的程序代碼。 此外, UnmanagedCode 許可權可能會允許略過其他許可權,而且應該只授與高度信任的程序代碼。
實例 PerformanceCounterCategory 的屬性 CategoryName 會顯示在 [性能查看器] 應用程式的 [新增計數器] 對話方塊的 [性能物件] 字段中。
類別 PerformanceCounterCategory 提供數種方法來與計算機上的計數器和類別互動。 方法 Create 可讓您定義自定義類別。 方法 Delete 提供從計算機移除類別的方法。 方法 GetCategories 可讓您檢視類別清單,同時 ReadCategory 擷取與單一類別相關聯的所有計數器和實例數據。
性能計數器會發佈應用程式的相關效能數據。 類別包括實體元件 (,例如處理器、磁碟和記憶體) ,以及系統物件 (,例如進程和線程) 。 與相同性能對象相關的系統計數器會分組為指出其共同焦點的類別。 當您建立 類別的 PerformanceCounter 實例時,請先指出元件將與其互動的類別,然後從該類別中選擇計數器。
例如,一個 Windows 計數器類別是 [記憶體] 類別。 此類別內的系統計數器會追蹤記憶體數據,例如可用的位元元組數目和快取的位元組數目。 如果您想要使用應用程式中快取的位元組,您可以建立元件的實例 PerformanceCounter 、將其連線到 [記憶體] 類別,然後在此案例中挑選適當的計數器 (,從該類別) 快取位元組。
雖然您的系統有更多計數器類別可供使用,但您可能最常與之互動的類別包括快取、記憶體、物件、PhysicalDisk、Process、Processor、Server、System 和 Thread 類別。
重要
類別 RemoveInstance 中的 PerformanceCounter 方法會釋放計數器,如果針對該類別選取了重複使用選項,則會重複使用計數器的實例。 如果另一個進程或甚至程序代碼的另一個部分嘗試寫入計數器實例,這可能會導致競爭狀況。
注意
強烈建議在安裝應用程式期間建立新的性能計數器類別,而不是在執行應用程式期間。 這可讓操作系統重新整理其已註冊性能計數器類別清單的時間。 如果清單尚未重新整理,則嘗試使用類別將會失敗。
注意
隨 .NET Framework 2.0 一起安裝的性能計數器類別會使用個別的共用記憶體,而每個性能計數器類別都有自己的記憶體。 您可以在登錄機碼中建立名為 FileMappingSize 的 DWORD,以指定個別共用記憶體的大小, <HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\類別名稱>\效能。 FileMappingSize 值會設定為類別的共用記憶體大小。 默認大小為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 |
取得或設定這個分類所在的電腦名稱。 |