知识大全 VB.Net实现进程监视器的方法

Posted

篇首语:知识以生命为前提,以经验为条件。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 VB.Net实现进程监视器的方法相关的知识,希望对你有一定的参考价值。

VB.Net实现进程监视器的方法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

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

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

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

  最终效果图

  

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

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

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

  添加命名空间

  Imports System Management

  Public Class HandleObjectReady

  Private plete As Boolean = false

  Private obj As ManagementBaseObject

  Public ReadOnly Property Complete As Boolean

  Get

  Return plete

  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)

  plete = true

  obj = e NewObject

  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 = searcher Get

  Dim observer As ManagementOperationObserver = New ManagementOperationObserver

  Dim hor As HandleObjectReady = New HandleObjectReady

  AddHandler observer ObjectReady AddressOf hor Done

  For Each mo As ManagementObject In moc

  mo InvokeMethod(observer GetOwner Nothing)

  While Not hor Complete

  System Threading Thread Sleep( )

  End While

  Dim user As String =

  (hor Obj( returnValue ) ToString = )

  user = hor Obj Properties( User ) Value ToString

  If Not Me mDict ContainsKey(pID) Then  Return

  End If

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

  AndAlso Me mDict ContainsKey(Convert ToInt (mo( ParentProcessID )))) Then

  Me mDict(pID) ParentProce = Me mDict(Convert ToInt (mo( ParentProcessID ))) ProceName

  End If

  Me mDict(pID) Creator = user

  If (Not (Me HandleDetailList) Is Nothing) Then

  Me HandleDetailList(Me mDict(pID))

  End If

  Next

  searcher Dispose

  searcher = Nothing

  moc Dispose

  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 计算

  (Tnew told)/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 System Diagnostics

  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)

  Me mCurrentTotalCpuTime = Me CalCurrentTotalCpuTime

  Dim i As Integer =

  Do While (i < pCurrentAll Length)

  If (pCurrentAll(i) Id = ) Then

  Me mDict(pCurrentAll(i) Id) CpuPercent = Me mIdleCpuPercent

  Else

  Try Dim ms As Long = CType(pCurrentAll(i) TotalProcessorTime TotalMilliseconds Long)

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

  &( / Me mCurrentTotalCpuTime))

  Me mDict(pCurrentAll(i) Id) CpuPercent = d

  Me mDict(pCurrentAll(i) Id) OldCpuTime = ms

  Catch  As System Exception

  End Try

  End If

  If (Not (Me HandleProceRefresh) Is Nothing) Then

  Me HandleProceRefresh(Me mDict(pCurrentAll(i) Id) ( Me mIdleCpuPercent))

  End If

  i = (i + )

  Loop

  End Sub

  Private Function CalCurrentTotalCpuTime() As Double

  Dim d As Double =

  Dim idlePercent As Double = mIdle NextValue

  Dim totalPercent As Double = mTotal NextValue

  If (totalPercent = ) Then

  Me mIdleCpuPercent =

  Else

  Me mIdleCpuPercent = (idlePercent * ( / totalPercent))

  End If

  For Each p As Process In Me mCurrentAll

  If ((p Id = )  _

  OrElse p HasExited) Then

   TODO Warning!!! continue If

  End If

  If ((Me mDict Is Nothing)  _

  OrElse Not Me mDict ContainsKey(p Id)) Then

  d = (d + p TotalProcessorTime TotalMilliseconds)

  Else

  d = (d  _

  + (p TotalProcessorTime TotalMilliseconds Me mDict(p Id) OldCpuTime))

  End If

  Next

  Return (d / ( mIdleCpuPercent))

cha138/Article/program/net/201311/12462

相关参考