最終效果圖
(以下給出部分代碼
(注意
添加命名空間
Imports System
Public Class HandleObjectReady
Private complete As Boolean = false
Private obj As ManagementBaseObject
Public ReadOnly Property Complete As Boolean
Get
Return complete
End Get
End Property
Public ReadOnly Property Obj As ManagementBaseObject
Get
Return obj
End Get
End Property
Public Sub Done(ByVal sender As Object
complete = true
obj = e
End Sub
End Class
Private Sub FillDetailUseWmi(ByVal pID As Integer)
Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher((
Dim moc As ManagementObjectCollection = searcher
Dim observer As ManagementOperationObserver = New ManagementOperationObserver
Dim hor As HandleObjectReady = New HandleObjectReady
AddHandler observer
For Each mo As ManagementObject In moc
mo
While Not hor
System
End While
Dim user As String =
(hor
user = hor
If Not Me
End If
If ((Not (mo(
AndAlso Me
Me
End If
Me
If (Not (Me
Me
End If
Next
searcher
searcher = Nothing
moc
moc = Nothing
observer = Nothing
hor = Nothing
End Sub
//通過計數器獲取idle空閒進程cpu占用率r
//通過process類的TotalProcessorTime屬性獲取各進程的cpu時間
//通過t
//對各進程
(Tnew
系統會為每個進程分配一個計數器
(第一次調用NextValue獲取的值都為
Idle意為懶散的
對於多核或使用超線程技術的cpu
添加命名空間
Imports System
Dim mIdle As PerformanceCounter = New PerformanceCounter(
Dim mTotal As PerformanceCounter = New PerformanceCounter(
Private Sub FillNeedRefreshInfo(ParamArray ByVal pCurrentAll() As Process)
Me
Dim i As Integer =
Do While (i < pCurrentAll
If (pCurrentAll(i)
Me
Else
Try Dim ms As Long = CType(pCurrentAll(i)
Dim d As Double = ((ms
&(
Me
Me
Catch As System
End Try
End If
If (Not (Me
Me
End If
i = (i +
Loop
End Sub
Private Function CalCurrentTotalCpuTime() As Double
Dim d As Double =
Dim idlePercent As Double = mIdle
Dim totalPercent As Double = mTotal
If (totalPercent =
Me
Else
Me
End If
For Each p As Process In Me
If ((p
OrElse p
End If
If ((Me
OrElse Not Me
d = (d + p
Else
d = (d _
+ (p
End If
Next
Return (d / (
End Function
From:http://tw.wingwit.com/Article/program/net/201311/12462.html