vb.net 获取CPU使用率

转载 2006年06月06日 21:46:00

 

copy一下代码到在模块下:

'API声明
Public Declare Function PdhVbGetOneCounterPath Lib "PDH.DLL" (ByVal PathString As String, ByVal PathLength As Integer, ByVal DetailLevel As Integer, ByVal CaptionString As String) As Integer
Public Declare Function PdhVbCreateCounterPathList Lib "PDH.DLL" (ByVal PERF_DETAIL As Integer, ByVal CaptionString As String) As Integer
Public Declare Function PdhVbGetCounterPathFromList Lib "PDH.DLL" (ByVal Index As Integer, ByVal Buffer As String, ByVal BufferLength As Integer) As Integer
Public Declare Function PdhOpenQuery Lib "PDH.DLL" (ByVal Reserved As Integer, ByVal dwUserData As Integer, ByRef hQuery As Integer) As PDH_STATUS
Public Declare Function PdhCloseQuery Lib "PDH.DLL" (ByVal hQuery As Integer) As PDH_STATUS
Public Declare Function PdhVbAddCounter Lib "PDH.DLL" (ByVal QueryHandle As Integer, ByVal CounterPath As String, ByRef CounterHandle As Integer) As PDH_STATUS
Public Declare Function PdhCollectQueryData Lib "PDH.DLL" (ByVal QueryHandle As Integer) As PDH_STATUS
Public Declare Function PdhVbIsGoodStatus Lib "PDH.DLL" (ByVal StatusValue As Integer) As Integer
Public Declare Function PdhVbGetDoubleCounterValue Lib "PDH.DLL" (ByVal CounterHandle As Integer, ByRef CounterStatus As Integer) As Double

' 枚举常数
Enum PERF_DETAIL
PERF_DETAIL_NOVICE = 100
PERF_DETAIL_ADVANCED = 200
PERF_DETAIL_EXPERT = 300
PERF_DETAIL_WIZARD = 400
End Enum

Enum PDH_STATUS
PDH_CSTATUS_VALID_DATA = &H0S
PDH_CSTATUS_NEW_DATA = &H1S
PDH_CSTATUS_NO_MACHINE = &H800007D0
PDH_CSTATUS_NO_INSTANCE = &H800007D1
PDH_MORE_DATA = &H800007D2
PDH_CSTATUS_ITEM_NOT_VALIDATED = &H800007D3
PDH_RETRY = &H800007D4
PDH_NO_DATA = &H800007D5
PDH_CALC_NEGATIVE_DENOMINATOR = &H800007D6
PDH_CALC_NEGATIVE_TIMEBASE = &H800007D7
PDH_CALC_NEGATIVE_VALUE = &H800007D8
PDH_DIALOG_CANCELLED = &H800007D9
PDH_CSTATUS_NO_OBJECT = &HC0000BB8
PDH_CSTATUS_NO_COUNTER = &HC0000BB9
PDH_CSTATUS_INVALID_DATA = &HC0000BBA
PDH_MEMORY_ALLOCATION_FAILURE = &HC0000BBB
PDH_INVALID_HANDLE = &HC0000BBC
PDH_INVALID_ARGUMENT = &HC0000BBD
PDH_FUNCTION_NOT_FOUND = &HC0000BBE
PDH_CSTATUS_NO_COUNTERNAME = &HC0000BBF
PDH_CSTATUS_BAD_COUNTERNAME = &HC0000BC0
PDH_INVALID_BUFFER = &HC0000BC1
PDH_INSUFFICIENT_BUFFER = &HC0000BC2
PDH_CANNOT_CONNECT_MACHINE = &HC0000BC3
PDH_INVALID_PATH = &HC0000BC4
PDH_INVALID_INSTANCE = &HC0000BC5
PDH_INVALID_DATA = &HC0000BC6
PDH_NO_DIALOG_DATA = &HC0000BC7
PDH_CANNOT_READ_NAME_STRINGS = &HC0000BC8
End Enum

Public Const ERROR_SUCCESS As Short = 0

Public Structure CounterInfo
Dim hCounter As Integer
Dim strName As String
End Structure


在窗体的load事件里面添加一下语句:
Dim pdhStatus As PDH_STATUS
  
pdhStatus = PdhOpenQuery(0, 1, hQuery)
If pdhStatus <> ERROR_SUCCESS Then
MsgBox("OpenQuery failed")
End
End If

在窗体的closed事件里面添加以下代码
PdhCloseQuery(hQuery)

' 显示利用率信息
 Private Sub UpdateValues()
  Dim dblCounterValue As Double
  Dim pdhStatus As Integer
  Dim strInfo As String
  Dim i As Integer
  
  PdhCollectQueryData(hQuery)
  
  i = 0
  dblCounterValue = PdhVbGetDoubleCounterValue(Counters(i).hCounter, pdhStatus)
  
  ' 将CPU利用率信息显示在标签中
  If (pdhStatus = PDH_STATUS.PDH_CSTATUS_VALID_DATA) or (pdhStatus = PDH_STATUS.PDH_CSTATUS_NEW_DATA) Then
strInfo = "CPU 使用率: " & VB6.Format(dblCounterValue, "0.00")
   pb1.Value = dblCounterValue
Me.Text = VB6.Format(dblCounterValue, "0") & "% - CPU 当前状态"
  End If
  
  Label1.Text = strInfo
 End Sub

相关文章推荐

solaris查看内存、CPU、硬盘总数和使用情况情况

solaris查看内存、CPU、硬盘总数和使用情况情况 转自:http://blog.chinaunix.net/uid-26454764-id-3145344.html 一、内存查询 ...
  • li19236
  • li19236
  • 2014年12月08日 18:35
  • 2123

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

VS2012自带的 性能分析工具使用包括CPU和内存等自定义详解

VS2012自带的 性能分析 工具使用实例 本篇通过一小段代码的console程序来进行性能的分析以及改进、直到后面的改进前、改进后性能比较结果。 先看console代码(源代码下...

【系统信息获取】5,获取CPU当前使用率

1,原理 CPU资源看做是一个个的时间片,统计CPU使用率也是计算在一段时间内忙碌的时间占比。 我们知道GetSystemTime可以得到当前系统时间,另外一个名字类似的函数,GetSyste...
  • dpsying
  • dpsying
  • 2014年02月10日 15:46
  • 4001

.NET获取当前程序所在电脑的CPU和内存使用率

using System; using System.Diagnostics; using System.Text; using System.Runtime.InteropServices; n...

【VB.NET】获取机器名称和IP地址

说明 操作步骤 获取本机 获取远程机器 总结说明 在接下俩的文章中主要和大家分享一下在VB.NET中如何通过System.NET以及System.NET.Sockets来获取本机和远程机器的名称以及I...

VB CPU使用率监控程序

  • 2011年12月23日 14:59
  • 41KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:vb.net 获取CPU使用率
举报原因:
原因补充:

(最多只允许输入30个字)