-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPUFrequencyForm.cs
More file actions
53 lines (42 loc) · 1.77 KB
/
CPUFrequencyForm.cs
File metadata and controls
53 lines (42 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Management;
using System.Windows.Forms;
using System.Windows.Threading;
namespace CPUFrequencyMonitor
{
public partial class CPUFrequencyForm : Form
{
ManagementObjectSearcher WMIWin32_Processor;
DispatcherTimer CpuFrequencyCollectionTimer;
const int uiUpdateIntervalMs = 1000;
const int cpuFrequencyCollectionIntervalMs = 10;
const int accuracy = uiUpdateIntervalMs / cpuFrequencyCollectionIntervalMs;
long cpuFrequencySum;
int cpuFrequencyCollectionIterationCounter;
public CPUFrequencyForm()
{
InitializeComponent();
WMIWin32_Processor = new ManagementObjectSearcher("select CurrentClockSpeed from Win32_Processor");
CpuFrequencyCollectionTimer = new DispatcherTimer();
CpuFrequencyCollectionTimer.Interval = new TimeSpan(0, 0, 0, 0, cpuFrequencyCollectionIntervalMs);
CpuFrequencyCollectionTimer.Tick += CollectCPUFrequency;
cpuFrequencySum = 0;
cpuFrequencyCollectionIterationCounter = 0;
CpuFrequencyCollectionTimer.Start();
}
private void CollectCPUFrequency(object sender, EventArgs e)
{
foreach (ManagementObject currentClockSpeedObject in WMIWin32_Processor.Get())
{
cpuFrequencySum += Convert.ToInt32(currentClockSpeedObject.GetPropertyValue("CurrentClockSpeed"));
}
cpuFrequencyCollectionIterationCounter++;
if (cpuFrequencyCollectionIterationCounter == accuracy)
{
textBoxFrequency.Text = (cpuFrequencySum / accuracy).ToString();
cpuFrequencyCollectionIterationCounter = 0;
cpuFrequencySum = 0;
}
}
}
}