c# - 创建新的 System.Diagnostics.PerformanceCounter 非常慢

标签 c# performancecounter

我有一个简单的监视应用程序,它从 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 秒。

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

最佳答案

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

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

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

关于c# - 创建新的 System.Diagnostics.PerformanceCounter 非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11612308/

相关文章:

C# 类检查文件夹中是否存在文件

profiling - Linux 内核中 Perf 事件子系统的性能开销

c# - .Net 中的负载测试应该遵循哪种方式?

c# - 如何在C#中正确使用Performance Counter或Process类来获取当前进程的内存使用情况?

c++ - 如何使用性能计数器控制从文件中读取?

c# - 为什么 WIA 看不到我的扫描仪?

c# - 在构建时如何设置 Gendarme 与 Teamcity 一起运行?

c# - Windows Phone 8 中图像的点击事件不起作用

没有方法重载的 c# 多态性(对于 web 方法)

C# 性能计数器和 nic 名称