Creating a new System.Diagnostics.PerformanceCounter is very slow

隐身守侯 提交于 2019-12-24 04:15:10

问题


I have a simple monitoring application that is getting some values from PerfMon counters. Even when testing on the local machine, it is taking over 30 seconds to create a new PerformanceCounter object.

using System;
using System.Diagnostics;

namespace test_slow_perfmon
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch w = new Stopwatch();

            w.Start();
            PerformanceCounter c = new PerformanceCounter("PhysicalDisk", "Avg. Disk Read Queue Length", "_Total", "localhost");
            w.Stop();
            Console.WriteLine(string.Format("Creating a counter took {0}ms", w.Elapsed.TotalMilliseconds));
        }
    }
}

Output from that indicates over 32s to create each counter.

What can I do (if anything) to speed up the creation of the counters?


回答1:


30 seconds sounds to me suspiciously like a timeout, indicating to me that this could be some sort of network issue.

Try creating your perfmon counter using the constructor that doesn't specify a hostname and see if that helps:

PerformanceCounter c = new PerformanceCounter("PhysicalDisk", "Avg. Disk Read Queue Length", "_Total");


来源:https://stackoverflow.com/questions/11612308/creating-a-new-system-diagnostics-performancecounter-is-very-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!