PerformanceCounterCategory.Exists is very slow if category doesn't exists

Posted by Shrike on Stack Overflow See other posts from Stack Overflow or by Shrike
Published on 2010-04-16T17:45:03Z Indexed on 2010/04/16 18:03 UTC
Read the original article Hit count: 333

Filed under:
|

I have kind of library which uses a bunch of its own perf counters. But I want my library works fine even if that perf counters weren't installed.

So I've created wrappers aroung PerformanceCounter and on the first use check if PerfCounter exists or not. If they exists then I'm using native PerformanceCounter instead I'm using a wrapper which do nothing.

So to check perf counter existence I use PerformanceCounterCategory.Exists

The problem is that if there isn't such category then PerformanceCounterCategory.Exists call takes (on my machine) about 10 seconds! Needless to say it's too slow.

What can I do?

Code to try it by youself: using System; using System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        var ts = Stopwatch.StartNew();
        var res = PerformanceCounterCategory.Exists("XYZ");
        Console.WriteLine(ts.ElapsedMilliseconds);
        Console.WriteLine("result:" + res);
}
}

© Stack Overflow or respective owner

Related posts about .NET

Related posts about system.diagnostics