VB.NET编程获得系统性能数据

编程爱好者

众所周知,在Windows XP中存在一个著名的性能监视器。作为计算机当前性能最直接的反映方式,它一直是各电脑玩家监视电脑性能最重要的帮手之一。如果需要查看它,则可以在“控制面板→管理工具→性能”中找到它(图1)。而本文将讨论的就是如何在Visual Basic.Net中访问这一计数器并从中获得所需要的资料。

47-f19-3.jpg
图1

一、访问计数器

下面我们可以通过一个例子看看VB.Net如何造访一个已存在的计数器。在例子的后面是关于这个例子的讲解:

'Notice:请将以下代码编译为CMD应用程序

Imports System

Imports System.Diagnostics

Public Class Test

Public Shared Sub Main()

If Performancecountercategory.Exists("Memory") Then

Dim testA As New PerformanceCounter("Memory",_

"Available Mbytes")

Console.WriteLine(testa.rawValue().ToString())

End If

End Sub

End Class

在这个例子中,我们通过位于system.diagnostics命名空间的类PerformanceCounterCategory的方法Exists确定了Processor类别的计数器存在与否,并通过PerformanceCounter类的一个实例造访了该计数器,通过RawValue属性提取出数据,并且显示在命令提示符状态下。

如果需要获得performanceCounter类构造函数的参数,可以在性能计数器窗口中右击,选择“添加计数器”,并选择相关的计数器类别和名称(图2)。本例中的计数器作用在于显示内存的使用情况。在这个例子中,由于类别Memory的计数器AvailableMbytes中只有一个实例,performanceCounter类构造函数只使用了两个参数。此时,该计数器是只读的。如果一个计数器包含多个实例,则performanceCounter类构造函数参数应遵循以下规则:

47-f19-4.jpg
图2

Dim x As performanceCounter("类别名","计数器名","实例名",Boolean)

当第四个参数为True时,该计数器是只读的;如果为False,则为只写的。当然,即使该构造函数的第二或第三个参数不填时,也可以使用这个布尔值决定计数器是只读或只写。

二、获取CPU使用率

需要注意的是,通过RawValue属性获取计数器值的方法有时并不是最得体的方法。如以下例子,我们通过NextSample方法经过计算获得CPU使用率:

Imports System

Imports System.Diagnostics

Public Class Test

Public Sub Test()

If Performancecountercategory.Exists("Processor") Then

Dim testA As New PerformanceCounter("Processor", "% Processor Time","_Total",True)

Dim a as CounterSample

Dim b as CounterSample

Dim c as single

a = testA.NextSample()

Thread.Sleep(1000)

b = testA.NextSample()

c = CounterSample.Calculate(a,b)

Console.WriteLine(c.ToString())

End If

End Sub

End Class

三、自定义性能计数器

除此之外,我们还可以通过.Net新建一个自定义的性能计数器。具体可以参见下面的例子:

Imports System

Imports System.Diagnostics

Public Class Test

Public Shared Sub Main()

If Not Performancecountercategory.Ex ists("Mine") Then

Dim MyCounter As New CounterCr eationData = New CounterCreatData("Catch MyExceptions","一个捕捉异常的例子",PerformanceCounterType.NumberOfItems32)

Dim Adder As counterCreationData Collection = New CounterCreationDataCollection()

Adder.Add(MyCounter)

PerformanceCounterCategory.Creat("MyCounter","CounterHelp",Adder)

End If

End Sub

End Class

这个例子应该相当容易看懂。通过CounterCreationData对需要创建的计数器进行描述,三个参数分别是计数器名称、计数器说明和一个PerformanceCounterType枚举。通过CounterCreationDataCollection收集描述,最后通过PerformanceCounterCategory类创建计数器类别,并指定计数器类别的名称。同时,一个CounterCreationDataCollection收集的CounterCreationData可以不止一个。唯一需要特别注意的是PerformanceCounterType枚举。该枚举提供的是NextValue的计算公式。其枚举值和意义请参阅MSDN,本文不再赘述。如果需要删除自定义的性能计数器,可以使用以下格式的代码:

PerformanceCounterCategory.Delete("MyCounter")

相信大家从上面的代码中也看出来了,这个计数器是用于记录某种异常的。那么我们下面当然就要建立一个异常,并使之具备使该计数器数值增加的能力。别忘了,现在PerformanceCounter的最后一个参数要设置成False了。这个异常类将从所有异常的基类ApplicationException中继承,有兴趣的读者可以查阅相关资料。

Public Class MyException

Inherits ApplicationException

Public Sub New()

If PerformaneCounterCategory.Exists("My Counter") Then

Dim TestB As New Performance Counter("MyCounter","CatchMyExceptions",_

False)

TestB.Increment()

End if

End Sub

End Class

可以使用Increment方法使计数器数值加1。下面就可以使用结构化异常处理测试自己创建的计数器了。别忘了把结果输出到命令提示符哟!

Try

Throw New MyException

Catch Error As MyException

If PerformaneCounterCategory.Exists("My Counter") Then

Dim TestC As New Performance Counter("MyCounter","CatchMyExceptions",True)

Console.Writeline(TeseC.RawValue().ToString())

End If

End Try