title: Why my PerformanceCounter always returns 0 when passing the counter to the method
body: <p>I've already found <a href="https://stackoverflow.com/questions/2181828/why-the-cpu-performance-counter-kept-reporting-0-cpu-usage">this question</a> telling that the first occurrence of my PerformanceCounter is 0, so I have to call it multiple times to get the right result.</p>
  
  <p>I got that to work fine - it looks like this:</p>
  
  <pre><code>public static float GetCpuUsage()
  {
      var cpuCounter = new PerformanceCounter();
  
      cpuCounter.CategoryName = "Processor";
      cpuCounter.CounterName = "% Processor Time";
      cpuCounter.InstanceName = "_Total";
  
      // Prime it
      cpuCounter.NextValue();
  
      Thread.Sleep(500);
  
      return cpuCounter.NextValue();
  }
  </code></pre>
  
  <p>However, I wish to be able to get the result from more than one PerformanceCounter without having to wait half a second for each one. I did it like this:</p>
  
  <pre><code>public static float GetCpuUsage(PerformanceCounter counter)
  {
      if (counter == null)
      {
          counter = new PerformanceCounter();
          counter.CategoryName = "Processor";
          counter.CounterName = "% Processor Time";
          counter.InstanceName = "_Total";
      }
  
      return counter.NextValue();
  }
  </code></pre>
  
  <p>And I call it like this:</p>
  
  <pre><code>PerformanceCounter cpuCounter = null;
  PerformanceCounter memoryCounter = null;
  
  while (_isRunning)
  {
      result.Cpu = GetCpuUsage(cpuCounter);
      result.Memory = GetAvailableMemory(memoryCounter);
  
      // Process result
  
      Thread.Sleep(500);
  }
  </code></pre>
  
  <p>I thought that was a pretty good idea - passing the same counter instance to the method every time.</p>
  
  <p>However, it doesn't actually work, cpu is always 0.</p>
  
  <p>It tells me there is something basic about PerformanceCounters I don't understand (and that is not addressed in the question I linked to).</p>
  
  <p>What exactly is it that makes the PerformanceCounter work? Because it seems that waiting for 500 milliseconds between each NextValue call is not the only difference.</p>