问题描述
我一直试图在C#中获取Windows PC(运行.Net 4.5的Windows 7)的总CPU使用率.看来使用PerformanceCounter应该可以满足我的需求.
I have been trying to get the total CPU usage of windows PC (Windows 7 running .Net 4.5) in C#. It looks like using PerformanceCounter should be able to meet my needs.
我根据下面的三个链接(并检查了msdn页面)编写了一些试用代码,这是最基本的版本:
I wrote some trial code based off the three links below (and checking the msdn pages), this was the most basic version:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace EntropyProject
{
class Program
{
static void Main(string[] args)
{
PerformanceCounter cpuCounter;
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
while(true)
{
try
{
float firstValue = cpuCounter.NextValue();
System.Threading.Thread.Sleep(500);
Console.WriteLine("Before getting processor:");
float currentCpuUsage = cpuCounter.NextValue();
Console.WriteLine("After getting processor:");
System.Threading.Thread.Sleep(1000);
Console.WriteLine(currentCpuUsage);
}
catch (Exception e)
{
Console.WriteLine("\n{0}\n", e.Message);
}
System.Threading.Thread.Sleep(10000);
}
}
}
}
每当调用NextValue时,都会触发以下异常错误.这似乎是性能计数器值存在问题的常见问题.
Whenever NextValue is called the exception error below is triggered. This appears to be a common problem with an issue with performance counter values.
最推荐解决方案建议您使用凸起的命令窗口中的lodctr作为管理员来重建损坏的项目.但是我想在一个程序中使用PerformanceCounters,该程序将被释放给很多人,因此期望他们使用命令窗口重建PerformanceCounters是不合适的.
Most recommended solutions suggest that you rebuild the corrupted items using lodctr in a raised command window as admin. However I was wanting to use the PerformanceCounters in a program which would be released to a large number of people and so would be inappropriate to expect them to rebuild their PerformanceCounters using the command window.
问题:
- 为什么会发生此异常错误?
- 否则如何正确使用PerformanceCounter?
- 如何避免让程序用户不得不打开cmd窗口并重建其性能计数器?
来源:
How to get cpu usage in C
关于访问计数器名称时的错误的类似问题安妮·谢赫(Annie Sheikh)问
Similar Question about error when accessing counter name asked by Annie Sheikh
推荐答案
根据 PerformanceCounter.NextValue ," 要读取性能计数器,您必须具有管理权限.文档还指出,如果" 在没有管理特权的情况下执行的代码试图读取性能计数器."
According to the documentation for PerformanceCounter.NextValue, "To read performance counters, you must have administrative privileges." The docs also state that an UnauthorizedAccessException will be thrown if "Code that is executing without administrative privileges attempted to read a performance counter."
但这是一个谎言.在运行.NET 4.5的Windows 7环境(64位家庭高级版)上,我可以通过管理员帐户,标准用户帐户甚至来宾帐户上未提升的cmd shell运行您的代码.
But this is a lie. On my Windows 7 environment (64-bit Home Premium edition), running .NET 4.5, I can run your code just fine from a non-raised cmd shell on an Administrator account, or a Standard User account, or even a Guest account.
如果您遇到特权问题,那么很可能没有大量的注册表摆弄将使您的代码为所有用户可靠地运行.但是,根据文档说明,我不清楚您的代码为什么在没有管理员特权的Guest帐户上可以正常工作.
If you are running into a privilege problem, then most likely no amount of registry fiddling is going to allow your code to run reliably for all users. But I'm unclear why your code works fine on a Guest account with no admin privileges, given what the documentation says.
另一个选择是运行"wmic.exe cpu get loadpercentage"并解析输出,如.根据我的测试,这可以通过管理员帐户或标准用户帐户运行,而不能通过访客帐户运行.
Another option would be to run "wmic.exe cpu get loadpercentage" and parse the output, as suggested in this answer. Based on my testing this works from either an Administrator or Standard User account, but not from a Guest account.
这篇关于如何在不让用户在命令提示符下使用lodctr进行重建的情况下使性能计数器正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!