Evening john. I'm a little stuck. I tried
Code:
If lvDisplayRunningProcesses.FindItemWithText(Process.ProcessName) Is Nothing Then
which in theory worked. But would stop duplicates of items such as svchost.exe etc
Here is my full code. (I did read what you posted, and it makes poerfect sense, just i'm a little confused.)
Code:
Private Sub DisplayCurrentlyRunningProcesses()
' lvDisplayRunningProcesses.Items.Clear()
Dim RunningProcesses() As Process = Process.GetProcesses()
Try
For Each Process In RunningProcesses
Dim ProcessID As Process = System.Diagnostics.Process.GetProcessById(Process.Id())
Dim lstThreads As ProcessThreadCollection = ProcessID.Threads
Dim lvi As ListViewItem = New ListViewItem()
Dim arryThreads As New List(Of Integer)
Dim Location As String = ProcessID.MainModule.FileName.ToString
cpuCounter = New PerformanceCounter()
cpuCounter.CategoryName = "Process"
cpuCounter.CounterName = "% Processor Time"
cpuCounter.InstanceName = Process.ProcessName
' Count all attached threads
For x As Integer = 0 To lstThreads.Count - 1
arryThreads.AddRange(New Integer() {x})
Next
If lvDisplayRunningProcesses.FindItemWithText(Process.ProcessName) Is Nothing Then
lvi.Text = Process.ProcessName & ".exe"
lvi.SubItems.Add(Process.Id.ToString())
lvi.SubItems.Add(cpuCounter.NextValue() & "%")
lvi.SubItems.Add(arryThreads.Count.ToString)
lvi.SubItems.Add(Process.WorkingSet64 / 1024 & " K")
lvi.SubItems.Add(Process.PriorityClass.ToString)
Try
lvi.SubItems.Add(Process.MainModule.FileVersionInfo.CompanyName)
Catch ex As ArgumentException
End Try
lvi.SubItems.Add(Location)
lvDisplayRunningProcesses.Items.Add(lvi)
End If
Next
Catch ex As Win32Exception
Catch ex As ArgumentOutOfRangeException
End Try
End Sub