none
Unable to create PerformanceCounter, "Category does not exist" RRS feed

  • Question

  • The following line of code used to be working ok until days ago when it started to generate exception,

    System.Diagnostics.PerformanceCounter _counter = new System.Diagnostics.PerformanceCounter("Memory""Available MBytes");

    Now it always throws the following excpeiton,

    Category does not exist.,"

       at System.Diagnostics.PerformanceCounterLib.CounterExists(String machine, String category, String counter)
       at System.Diagnostics.PerformanceCounter.InitializeImpl()
       at System.Diagnostics.PerformanceCounter..ctor(String categoryName, String counterName, String instanceName, Boolean readOnly)
       at System.Diagnostics.PerformanceCounter..ctor(String categoryName, String counterName)

    I can reproduce this problem in a very simple WPF application, with that line of code as the only change to an empty project. Could anyone think of what has gone wrong? I've tried various ways from web like rebuilding performance counter list of the os but still can't get rid of it. I'm running as admin on my local computer.

    Thanks.


    Cheers, Daniel


    • Edited by Daniel_ngn Wednesday, February 20, 2013 6:18 AM
    Wednesday, February 20, 2013 6:18 AM

All replies

  • You should create Category for your counters at first, when you register a new counter as shown below:

    private static bool SetupCategory()
    	{		
    		if ( !PerformanceCounterCategory.Exists("AverageCounter64SampleCategory") ) 
    		{
    
    			CounterCreationDataCollection counterDataCollection = new CounterCreationDataCollection();
    			
    			// Add the counter.
    			CounterCreationData averageCount64 = new CounterCreationData();
    			averageCount64.CounterType = PerformanceCounterType.AverageCount64;
    			averageCount64.CounterName = "AverageCounter64Sample";
    			counterDataCollection.Add(averageCount64);
    	        
    	        // Add the base counter.
    			CounterCreationData averageCount64Base = new CounterCreationData();
    			averageCount64Base.CounterType = PerformanceCounterType.AverageBase;
    			averageCount64Base.CounterName = "AverageCounter64SampleBase";
    			counterDataCollection.Add(averageCount64Base);
    
    			// Create the category.
    			PerformanceCounterCategory.Create("AverageCounter64SampleCategory",
                    "Demonstrates usage of the AverageCounter64 performance counter type.",
                    PerformanceCounterCategoryType.SingleInstance, counterDataCollection);
    				
    			return(true);
    		}
    		else
    		{
    			Console.WriteLine("Category exists - AverageCounter64SampleCategory");
    			return(false);
    		}
    	}

    • Proposed as answer by Anton Matyushov Thursday, February 21, 2013 2:49 PM
    • Unproposed as answer by Daniel_ngn Friday, February 22, 2013 12:36 AM
    Thursday, February 21, 2013 2:49 PM
  • Thanks Anton, but I'm using a system built-in counter instead of my own.

    Cheers, Daniel

    Friday, February 22, 2013 12:36 AM
  • Try this: http://social.msdn.microsoft.com/Forums/en/clr/thread/35420846-a7b4-4298-afbc-596f3815a125 

    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Monday, February 25, 2013 8:52 AM
  • I'm trying to access a local performance counter.

    Cheers, Daniel

    Monday, February 25, 2013 8:55 AM
  • If so, that should be you don't have permission on that category.

    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Thursday, February 28, 2013 1:19 PM
  • Please check it does exist or not: http://msdn.microsoft.com/en-us/library/zbthbc75(v=vs.90).aspx 

    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Thursday, February 28, 2013 1:22 PM