title: What would make PerformanceCounterCategory.Exists hang indefinitely?
body: <p>I've got an application that uses performance counters, that has worked for months. Now, on my dev machine and another developers machine, it has started hanging when I call PerformanceCounterCategory.Exists. As far as I can tell, it hangs indefinitely. It does not matter which category I use as input, and other applications using the API exhibits the same behaviour.</p>
  
  <p>Debugging (using MS Symbol Servers) has shown that it is a call to Microsoft.Win32.RegistryKey that hangs. Further investigation shows that it is this line that hangs:</p>
  
  <pre><code>while (Win32Native.ERROR_MORE_DATA == (r = Win32Native.RegQueryValueEx(hkey, name, null, ref type, blob, ref sizeInput))) { 
  </code></pre>
  
  <p>This is basically a loop that tries to allocate enough memory for the performance counter data. It starts at <code>size = 65000</code> and does a few iterations. In the 4th call, when <code>size = 520000</code>, <code>Win32Native.RegQueryValueEx</code> hangs.</p>
  
  <p>Furthermore, rather worringly, I found this comment in the reference source for PerformanceCounterLib.GetData:</p>
  
  <pre><code>    // Win32 RegQueryValueEx for perf data could deadlock (for a Mutex) up to 2mins in some 
      // scenarios before they detect it and exit gracefully. In the mean time, ERROR_BUSY,
      // ERROR_NOT_READY etc can be seen by other concurrent calls (which is the reason for the 
      // wait loop and switch case below). We want to wait most certainly more than a 2min window. 
      // The curent wait time of up to 10mins takes care of the known stress deadlock issues. In most
      // cases we wouldn't wait for more than 2mins anyways but in worst cases how much ever time 
      // we wait may not be sufficient if the Win32 code keeps running into this deadlock again
      // and again. A condition very rare but possible in theory. We would get back to the user
      // in this case with InvalidOperationException after the wait time expires.
  </code></pre>
  
  <p>Has anyone seen this behaviour before ? What can I do to resolve this ? </p>