WPF 获取系统CPU实时速度
时间: 2023-08-28 20:05:25 浏览: 55
你可以使用 System.Management 命名空间下的 ManagementObjectSearcher 类来获取系统的 CPU 实时速度。具体步骤如下:
1. 引入 System.Management 命名空间。
2. 创建 ManagementObjectSearcher 对象并设置查询条件。
3. 使用 foreach 循环遍历查询结果,获取 CPU 实时速度。
具体代码如下:
```csharp
using System.Management;
// ...
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Processor");
foreach (ManagementObject obj in searcher.Get())
{
double speed = Convert.ToDouble(obj["CurrentClockSpeed"]) / 1000.0; // 单位 GHz
Console.WriteLine($"CPU 实时速度:{speed} GHz");
}
```
注意:此方法获取的是 CPU 实时速度,而非 CPU 的最大时钟频率。
相关问题
WPF 准确获取系统 CPU 利用率 每秒更新
在 WPF 中,可以使用 System.Diagnostics 命名空间下的 PerformanceCounter 类和 System.Windows.Threading 命名空间下的 DispatcherTimer 类来获取系统 CPU 利用率,并实现每秒更新。具体步骤如下:
1. 引入 System.Diagnostics 和 System.Windows.Threading 命名空间:
```
using System.Diagnostics;
using System.Windows.Threading;
```
2. 创建 PerformanceCounter 对象:
```
PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
```
其中,第一个参数 "Processor" 表示 CPU 监视器,第二个参数 "% Processor Time" 表示 CPU 使用率,第三个参数 "_Total" 表示所有 CPU 核心的总体使用率。
3. 创建 DispatcherTimer 对象:
```
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
```
其中,TimeSpan.FromSeconds(1) 表示每隔 1 秒触发一次 Tick 事件。
4. 绑定 Tick 事件处理方法:
```
timer.Tick += new EventHandler(OnTimerTick);
```
5. 实现 Tick 事件处理方法,读取并更新 CPU 使用率:
```
void OnTimerTick(object sender, EventArgs e)
{
float cpuUsage = cpuCounter.NextValue();
Console.WriteLine("CPU 使用率:{0}%", cpuUsage);
}
```
注意,第一次调用 NextValue 方法获取的是 0,因为需要先获取一次初始值,之后才能获取正确的 CPU 使用率。
6. 启动 Timer:
```
timer.Start();
```
完整示例代码如下:
```
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;
public partial class MainWindow : Window
{
private PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
public MainWindow()
{
InitializeComponent();
// 获取初始值
cpuCounter.NextValue();
// 创建 Timer
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += new EventHandler(OnTimerTick);
timer.Start();
}
void OnTimerTick(object sender, EventArgs e)
{
// 获取 CPU 使用率
float cpuUsage = cpuCounter.NextValue();
Console.WriteLine("CPU 使用率:{0}%", cpuUsage);
}
}
```
注意,由于 WPF 应用程序是单线程的,因此不需要将 PerformanceCounter 对象定义为静态变量。
wpf 获取某个应用的cpu占有率
可以使用 System.Diagnostics 命名空间下的 Process 类实现获取某个应用的 CPU 占有率。
以下是一个示例代码:
```csharp
using System.Diagnostics;
// 获取进程的 CPU 占有率
public static float GetCpuUsage(Process process)
{
var cpuCounter = new PerformanceCounter("Process", "% Processor Time", process.ProcessName, true);
cpuCounter.NextValue();
System.Threading.Thread.Sleep(1000);
return cpuCounter.NextValue() / Environment.ProcessorCount;
}
// 示例代码,获取进程名为 notepad++ 的应用的 CPU 占有率
var processes = Process.GetProcessesByName("notepad++");
if (processes.Length > 0)
{
var process = processes[0];
var cpuUsage = GetCpuUsage(process);
Console.WriteLine($"notepad++ 占用 CPU 的百分比为:{cpuUsage:P}");
}
```
此代码可以获取进程名为 notepad++ 的应用的 CPU 占有率,你可以将其修改为你所需监控的应用程序名字。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)