-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessCpuUsage.cs
More file actions
48 lines (37 loc) · 1.28 KB
/
ProcessCpuUsage.cs
File metadata and controls
48 lines (37 loc) · 1.28 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
using System;
using System.Diagnostics;
namespace ProcessCpuUsageStatusWindow;
public class ProcessCpuUsage
{
public string ProcessName { get; private set; }
public float PercentUsage { get; private set; }
public DateTime LastFound { get; private set; }
public bool UsageValid { get; private set; }
private CounterSample LastSample { get; set; }
internal ProcessCpuUsage(InstanceData instanceData)
: this(instanceData, DateTime.MinValue)
{
}
internal ProcessCpuUsage(InstanceData instanceData, DateTime timestamp)
{
// Store the process details
ProcessName = instanceData.InstanceName;
// Store the initial data
LastFound = timestamp;
LastSample = instanceData.Sample;
// We start out as not valid
UsageValid = false;
}
internal void UpdateCpuUsage(InstanceData instanceData, DateTime timestamp)
{
// Get the new sample
var newSample = instanceData.Sample;
// Calculate percent usage
PercentUsage = CounterSample.Calculate(LastSample, newSample) / Environment.ProcessorCount;
// Update the last sample and timestamp
LastSample = newSample;
LastFound = timestamp;
// Usage is now valid
UsageValid = true;
}
}