title: PerformanceCounter multiple Processes
body: <p>I'm building a fast process cpu usage detection program and I'm getting a little issue here with PerformanceCounter.</p>
  
  <p>If i add a PerformanceCounter object and put the right values on the properties separator on GUI I have this working. But this work just for 1 fixed process. So what I'm trying to do is a dynamic way to get the values.
  Look:</p>
  
  <pre><code>Private Function getCPUByProcessName(ByVal proc As String) as Single
          Return New PerformanceCounter("Process", "% Processor Time", proc).NextValue()
  End Function
  </code></pre>
  
  <p>This function must return the % and it doesn't. If I try to get a fixed process % cpu usage by coding on the class It doesn't work. But if I just go to the GUI and add from toolbox and edit the properties to contribute to the laziness it works. :/</p>
  
  <p>TL;DR: the function above doesn't work. returns always 0.0</p>
  
  <p>CORRECTED CODE:</p>
  
  <pre><code>Public ProcDic As New Dictionary(Of Integer, PerformanceCounter)
  
  Private Function getCPUByProcess(ByRef proc As Process) As Single
          If Not ProcDic.ContainsKey(proc.Id) Then
              ProcDic.Add(proc.Id, New PerformanceCounter("Process", "% Processor Time",proc.ProcessName))
          End If
          Return ProcDic.Item(proc.Id).NextValue()
  End Function
  </code></pre>