【问题标题】:Creating a new System.Diagnostics.PerformanceCounter is very slow创建一个新的 System.Diagnostics.PerformanceCounter 非常慢
【发布时间】:2012-07-23 12:13:35
【问题描述】:

我有一个简单的监控应用程序,它从 PerfMon 计数器中获取一些值。即使在本地机器上进行测试,创建一个新的PerformanceCounter 对象也需要 30 多秒。

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));
        }
    }
}

其中的输出表明创建每个计数器的时间超过 32 秒。

我可以做些什么(如果有的话)来加快计数器的创建速度?

【问题讨论】:

  • 32 秒???尝试“。”时是否更快?作为“localhost”的机器名称?

标签: c# performancecounter


【解决方案1】:

30 秒对我来说听起来像是超时,这表明这可能是某种网络问题。

尝试使用the constructor that doesn't specify a hostname 创建您的性能计数器,看看是否有帮助:

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

【讨论】:

  • 好像是这样。如果我没有指定主机名,或者使用“。”对于本地主机,它可以找到。只是“本地主机”似乎无法正确解析。
猜你喜欢
  • 1970-01-01
  • 2012-02-08
  • 2013-08-28
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多