我只是想获得与任务管理器匹配的准确CPU使用率.到目前为止,我已经尝试了四种不起作用的推荐方法.
首先,我尝试了类似的解决方案或建议,我可以找到.这里的代码示例使用四种方法,所有方法都不准确.其次,我知道任务管理器会波动并取决于它何时被采样.这仍然没有考虑到差异.最后,我知道有不同的方法,任务管理器只使用一种方法.由于这适用于一般用户,因此需要靠近任务管理器.
public partial class MainWindow : Window
{
//*** Method 1 & 2
PerformanceCounter cpuCounterPi = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
PerformanceCounter cpuCounterP = new PerformanceCounter("Processor", "% Processor Time", "_Total");
//*** method 3
ManagementObjectSearcher query1 = new ManagementObjectSearcher("select loadpercentage from win32_processor");
//*** Mixed method usage below
CounterSample csPi1, csPi2, csP1, csP2;
double cpuPercentagePi, cpuPercentageP, cpuPercentageLoad;
int count = -1;
Boolean alternate = false;
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
public MainWindow()
{
InitializeComponent();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval …Run Code Online (Sandbox Code Playgroud)