-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessCpuUsage.cs
60 lines (44 loc) · 1.55 KB
/
ProcessCpuUsage.cs
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
54
55
56
57
58
59
60
using System;
using System.Diagnostics;
namespace ProcessCpuUsageStatusWindow
{
public class ProcessCpuUsage
{
#region Properties
public string ProcessName { get; private set; }
public float PercentUsage { get; internal set; }
public DateTime LastFound { get; set; }
public bool UsageValid { get; private set; }
internal CounterSample LastSample { get; set; }
#endregion
#region Constructor
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;
}
#endregion
#region Usage update
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 timestmap
LastSample = newSample;
LastFound = timestamp;
// Usage is now valid
UsageValid = true;
}
#endregion
}
}