David Klempfner
David Klempfner

Reputation: 9940

Performance Counter Not Displaying

I have this program. I have tried to view the counters in perfmon.exe after running this program, however the counters don't change in Performance Monitor.

I'm running Win 8.1 with latest .NET, and have run Visual Studio as admin.

class Program
{
    static void Main(string[] args)
    {
        if (CreatePerformanceCounters())
        {
            Console.WriteLine("Created performance counters");
            Console.WriteLine("Please restart application");
            Console.ReadKey();
            return;
        }
        var totalOperationsCounter = new PerformanceCounter("MyCategory", "# operations executed", "", false);
        var operationsPerSecondCounter = new PerformanceCounter("MyCategory", "# operations / sec", "", false);
        totalOperationsCounter.Increment();
        operationsPerSecondCounter.Increment();
    }
    private static bool CreatePerformanceCounters()
    {
        if (!PerformanceCounterCategory.Exists("MyCategory"))
        {
            CounterCreationDataCollection counters = new CounterCreationDataCollection 
            { 
                new CounterCreationData("# operations executed", "Total number of operations executed", PerformanceCounterType.NumberOfItems32), 
                new CounterCreationData("# operations / sec", "Number of operations executed per second", PerformanceCounterType.RateOfCountsPerSecond32)
            };
            PerformanceCounterCategory.Create("MyCategory", "Sample category for Codeproject", counters);
            return true;
        }
        return false;
    }
}

enter image description here

Upvotes: 1

Views: 120

Answers (1)

Brian Rasmussen
Brian Rasmussen

Reputation: 116481

Your app exits immediately after writing the counters. Try keeping the process alive a bit longer and you'll see the counters in perfmon.

Upvotes: 4

Related Questions