vb.net

位置:IT落伍者 >> vb.net >> 浏览文章

VB.Net实现进程监视器的方法


发布日期:2019年09月17日
 
VB.Net实现进程监视器的方法

)可以查看进程的各项基本信息如cpu内存父进程执行路径创建者等

)可以中止进程创建新进程

)可以配置目标进程配置刷新速度

最终效果图

(以下给出部分代码其余像进程的创建中止等使用process类将很容易实现)

)使用wmi获取父进程id进程创建者

(注意使用wmi获得的内容不宜循环刷新这样代价比较大)

添加命名空间

Imports SystemManagement

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 ByVal e As ObjectReadyEventArgs)

complete = true

obj = eNewObject

End Sub

End Class

Private Sub FillDetailUseWmi(ByVal pID As Integer)

Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher((Select * From Win_Process Where ProcessID= + pID))

Dim moc As ManagementObjectCollection = searcherGet

Dim observer As ManagementOperationObserver = New ManagementOperationObserver

Dim hor As HandleObjectReady = New HandleObjectReady

AddHandler observerObjectReady AddressOf horDone

For Each mo As ManagementObject In moc

moInvokeMethod(observer GetOwner Nothing)

While Not horComplete

SystemThreadingThreadSleep(

End While

Dim user As String =

(horObj(returnValueToString =

user = horObjProperties(UserValueToString

If Not MemDictContainsKey(pID) ThenReturn

End If

If ((Not (mo(ParentProcessID)) Is Nothing) _

AndAlso MemDictContainsKey(ConvertToInt(mo(ParentProcessID)))) Then

MemDict(pID)ParentProce = MemDict(ConvertToInt(mo(ParentProcessID)))ProceName

End If

MemDict(pID)Creator = user

If (Not (MeHandleDetailList) Is Nothing) Then

MeHandleDetailList(MemDict(pID))

End If

Next

searcherDispose

searcher = Nothing

mocDispose

moc = Nothing

observer = Nothing

hor = Nothing

End Sub

)使用性能计数器计算cpu利用率

)计算过程

//通过计数器获取idle空闲进程cpu占用率r

//通过process类的TotalProcessorTime属性获取各进程的cpu时间求和得各进程(除空闲进程idle该进程无法通过process类获得cpu时间)cpu时间和t

//通过t/(r)得到总cpu时间t

//对各进程通过TotalProcessorTime获得进程cpu时间tnew计算

(Tnewtold)/t即得该进程的cpu占用率其中told是程序中记录的该进程上一次的TotalProcessorTime

)关于性能计数器

)关于性能计数器

系统会为每个进程分配一个计数器通过new PerformanceCounter(Process % Processor Time 进程名称)实例化该计数器使用计数器对象的NextValue方法可以得到进程占用cpu的百分比

(第一次调用NextValue获取的值都为之后就没问题了这个要注意)

)Idle进程的含义

Idle意为懒散的无所事事事实上idle不能算着一个进程它用于表示cpu空闲资源它所占的比率越高表示你的机器越空闲

)多核CPU或使用超线程技术的CPU

对于多核或使用超线程技术的cpu根据计数器求得的idle进程cpu占用比率将超过%此时应将idle的cpu利用率/总的cpu利用率所得作为真正的idle的cpu利用率

添加命名空间

Imports SystemDiagnostics

Dim mIdle As PerformanceCounter = New PerformanceCounter(Process % Processor Time Idle

Dim mTotal As PerformanceCounter = New PerformanceCounter(Process % Processor Time _Total

Private Sub FillNeedRefreshInfo(ParamArray ByVal pCurrentAll() As Process)

MemCurrentTotalCpuTime = MeCalCurrentTotalCpuTime

Dim i As Integer =

Do While (i < pCurrentAllLength)

If (pCurrentAll(i)Id = ) Then

MemDict(pCurrentAll(i)Id)CpuPercent = MemIdleCpuPercent

Else

Try Dim ms As Long = CType(pCurrentAll(i)TotalProcessorTimeTotalMillisecondsLong)

Dim d As Double = ((ms MemDict(pCurrentAll(i)Id)OldCpuTime) * _

&( / MemCurrentTotalCpuTime))

MemDict(pCurrentAll(i)Id)CpuPercent = d

MemDict(pCurrentAll(i)Id)OldCpuTime = ms

Catch As SystemException

End Try

End If

If (Not (MeHandleProceRefresh) Is Nothing) Then

MeHandleProceRefresh(MemDict(pCurrentAll(i)Id) MemIdleCpuPercent))

End If

i = (i +

Loop

End Sub

Private Function CalCurrentTotalCpuTime() As Double

Dim d As Double =

Dim idlePercent As Double = mIdleNextValue

Dim totalPercent As Double = mTotalNextValue

If (totalPercent = ) Then

MemIdleCpuPercent =

Else

MemIdleCpuPercent = (idlePercent * ( / totalPercent))

End If

For Each p As Process In MemCurrentAll

If ((pId = ) _

OrElse pHasExited) Then

TODO Warning!!! continue If

End If

If ((MemDict Is Nothing) _

OrElse Not MemDictContainsKey(pId)) Then

d = (d + pTotalProcessorTimeTotalMilliseconds)

Else

d = (d _

+ (pTotalProcessorTimeTotalMilliseconds MemDict(pId)OldCpuTime))

End If

Next

Return (d / ( mIdleCpuPercent))

End Function

上一篇:使用VB.Net查找程序的前一个运行结果

下一篇:在VB.NET编程中使用数组