Vision

Disappearing performance counter category

January 2006

Recently we've built a windows service which executes multiple tasks in different threads. The windows service checks current CPU usage (amongst other things) to determine if a new task thread is spawned or not. That code looks something like this:

PerformanceCounter objCounter = new PerformanceCounter();
objCounter.CategoryName = "Processor";
objCounter.CounterName = "% Processor Time";
objCounter.InstanceName = "_Total";
objCounter.MachineName = ".";
objCounter.ReadOnly = true;

int intCurrentCpuUsage = Convert.ToInt32(objCounter.NextValue());

This code has worked fine for a couple of weeks, but at some point we were getting all these errors saying "The category doesn't exist." After checking the Visual Studio.NET Server Explorer we noticed the Processor category disappeared there too.

We still don't what caused this category to disappear, but we found a nice tool which helped in solving the issue, the Extensible Performance Counter List (Exctrlst.exe). This tool is a part of the Windows 2000 Resource Kit, and although our development machine is Windows XP it seemed to work fine. The tool can be downloaded from: http://www.microsoft.com/windows2000/techinfo/reskit/tools/existing/exctrlst-o.asp.

We started the tool and located PerfOS (perfos.dll) and checked the 'Performance Counters Enabled' checkbox. After that the Processor category was available again and the errors stopped!

Comments?

If you have comments, we'd love to hear it! Drop us a line at info@lcbridge.nl.

Additional comment

April 11, 2006 - The first NextValue() call always results in 0. The second time returns the actual CPU usage. So, don’t create new instances of PerformanceCounter every time you want to retrieve the CPU usage. Instead, create a static member which contains an instance of the PerformanceCounter class.

« back to overview page