Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么调用PerformanceCounter很慢?_C#_.net_Windows_Performance_Performancecounter - Fatal编程技术网

C# 为什么调用PerformanceCounter很慢?

C# 为什么调用PerformanceCounter很慢?,c#,.net,windows,performance,performancecounter,C#,.net,Windows,Performance,Performancecounter,我使用的服务器从PerformanceCounters读取“%CPU”和“可用内存”。 它在我的开发机器上运行良好。但在实际服务器上,读取这两个性能计数器的速度非常慢。至少前两次读取操作。 执行以下代码可能需要4-6分钟: Stopwatch watch = new Stopwatch(); Log.Instance.Debug("Going to initialize Diagnostic's PerformanceCounters"); w

我使用的服务器从PerformanceCounters读取“%CPU”和“可用内存”。 它在我的开发机器上运行良好。但在实际服务器上,读取这两个性能计数器的速度非常慢。至少前两次读取操作。
执行以下代码可能需要4-6分钟:

        Stopwatch watch = new Stopwatch();

        Log.Instance.Debug("Going to initialize Diagnostic's PerformanceCounters");

        watch.Start();

        m_CPUPerformanceCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
        m_MemoryPerformanceCounter = new PerformanceCounter("Memory", "Available MBytes", true);

        m_CPUPerformanceCounter.NextValue();
        m_MemoryPerformanceCounter.NextValue();

        //Ensure an updated value...
        System.Threading.Thread.Sleep(1000);

        m_CPUPerformanceCounter.NextValue();
        m_MemoryPerformanceCounter.NextValue();

        watch.Stop();

        Log.Instance.Debug("Finished initializing Diagnosticss PerformanceCounters. Time elapsed: {0}", watch.Elapsed);  
Finished initializing Diagnosticss PerformanceCounters. Time elapsed: 00:03:59.6706860
当我在我的开发机器上运行这段代码时,它将在不到2秒钟内完成(有时甚至更短)。但在我们产品的客户机应该使用的实际服务器上,这将需要很长时间。见下文:

        Stopwatch watch = new Stopwatch();

        Log.Instance.Debug("Going to initialize Diagnostic's PerformanceCounters");

        watch.Start();

        m_CPUPerformanceCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
        m_MemoryPerformanceCounter = new PerformanceCounter("Memory", "Available MBytes", true);

        m_CPUPerformanceCounter.NextValue();
        m_MemoryPerformanceCounter.NextValue();

        //Ensure an updated value...
        System.Threading.Thread.Sleep(1000);

        m_CPUPerformanceCounter.NextValue();
        m_MemoryPerformanceCounter.NextValue();

        watch.Stop();

        Log.Instance.Debug("Finished initializing Diagnosticss PerformanceCounters. Time elapsed: {0}", watch.Elapsed);  
Finished initializing Diagnosticss PerformanceCounters. Time elapsed: 00:03:59.6706860
重要的是,这些读取操作(以及以后的“读取”操作)将执行得非常快。甚至在开始的时候

我的开发机器是Windows 7、64位、8 GB RAM。
客户端服务器是Windows server 2008 R2 Enterprise,64位,4 GB RAM

我在谷歌上搜索了一下,但找不到任何答案。
为什么会这样?我怎样才能修好它

问题最有可能是安装在受监控系统上的其他软件提供的计数器


在这种情况下,您应该对系统注册表中注册的所有性能计数器执行性能计数器初始化扫描,这可以通过ProcMon完成

我不知道问题出在哪里,但这是:
//确保更新的值。。。系统线程线程睡眠(1000)
绝对不能确保值得到更新。为什么在只读模式下将计数器指定为访问?作为测试,请尝试使用新的PerformanceCounter(“处理器”、“处理器时间”、“总计”):你确定你没有在这里隐藏异常吗?例如UnauthorizedAccessException@Tarec你是对的。但是,如果需要,操作系统有足够的时间更新这些性能计数器。@RBarryYoung经过长时间的初始化后,每个读取操作都会执行得非常快。每次服务(我构建的,而不是操作系统)重新启动时,缓慢的操作都会重现。是否重新启动VM无关紧要。网络问题可能来自内部尝试联系PDC/BDC并授权您的服务访问性能信息。