Locked Strange behaivour of process counters

  • Tuesday, October 27, 2009 10:16 AM
     
      Has Code
    Hi, All!

    We have web application in which we are using some performance counters. For cases when there are more then one application run simultaneously we have code for determining current instance name:
      int processId = GetProcessID();
    
      PerformanceCounterCategory processCategory = new PerformanceCounterCategory("Process");
      string[] runnedInstances = processCategory.GetInstanceNames();
    
      foreach (string runnedInstance in runnedInstances){
        using (System.Diagnostics.PerformanceCounter performanceCounter = 
          new System.Diagnostics.PerformanceCounter("Process", "ID Process", runnedInstance, true)){
          if ((int)performanceCounter.RawValue == processId){
            return runnedInstance;
          }
        }
      }
    This code works fine but some time ago we added in our support of two counters from .Net CLR category. And as we discovered they have different match between process id and instance name: illustrated here

    So Process Id's are differs for one instance name for .Net Clr Memory and Process category.

       1. Is it bug of .Net?
       2. What counter(Process ID from .Net Clr Memory or ID Process from Process category) should I use to get right Instance name?