熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

VB.Net實現進程監視器的方法

2022-06-13   來源: .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) Then  Return

  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


From:http://tw.wingwit.com/Article/program/net/201311/12462.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.