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