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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Diagnostics;
using System.Threading;
/// <summary>
/// Mit dieser Klassen kann man die CPU Last von einer oder mehreren CPUs überwachen.
/// </summary>
public class CPULoadWatcher
{
/// <summary>
/// Dieses Event wird von jedem hinzugefügtem Watcher in den vorgegebenen
/// Zeitintervallen aufgerufen und übergibt die aktuelle CPU Last.
/// Hinweis: Der Parameter e wird von einem anderen Thread aus übergeben.
/// </summary>
public event CPULoadChangedEventHandler CPULoadChanged;
public delegate void CPULoadChangedEventHandler(CPULoadEventArgs e);
private System.Collections.Generic.Dictionary<ushort, Watcher> WatcherCollection = new System.Collections.Generic.Dictionary<ushort, Watcher>();
/// <summary>
/// Fügt einen Watcher für eine CPU hinzu
/// </summary>
/// <param name="CPU">Die Nummer der CPU die überwacht werden soll</param>
/// <param name="Interval">Gibt an in welchen Zeitabständen (ms) die CPU Last ausgelesen werden soll. Empfohlen: 1000ms</param>
public void AddWatcher(ushort CPU, int Interval)
{
if (WatcherCollection.ContainsKey(CPU) == false)
{
Watcher W = new Watcher(CPU, Interval);
W.Changed += OnCPULoadChanged;
WatcherCollection.Add(CPU, W);
W.Start();
}
}
/// <summary>
/// Entfernt einen Watcher für eine CPU
/// </summary>
/// <param name="CPU">Die Nummer der CPU dessen Watcher entfernt werden soll</param>
public void RemoveWatcher(ushort CPU)
{
if (WatcherCollection.ContainsKey(CPU) == true)
{
WatcherCollection[CPU].Abort();
WatcherCollection.Remove(CPU);
}
}
private void OnCPULoadChanged(CPULoadEventArgs e)
{
if (CPULoadChanged != null)
{
CPULoadChanged(e);
}
}
private class Watcher
{
private ushort mCPU = 0;
private int mInterval;
private bool mAbort = false;
private PerformanceCounter PerfCounter;
public event ChangedEventHandler Changed;
public delegate void ChangedEventHandler(CPULoadEventArgs e);
public Watcher(ushort CPU, int Interval)
{
mCPU = CPU;
mInterval = Interval;
}
public void Start()
{
PerfCounter = new PerformanceCounter("Processor", "% Processor Time", mCPU.ToString());
System.Threading.Thread T = new System.Threading.Thread(Watch);
T.Start();
}
public void Abort()
{
mAbort = true;
}
private void Watch()
{
do
{
if (mAbort == true)
{
mAbort = false;
break;
}
if (Changed != null)
{
Changed(new CPULoadEventArgs(mCPU, PerfCounter.NextValue()));
}
System.Threading.Thread.Sleep(mInterval);
}
while (true);
}
}
public class CPULoadEventArgs
{
private ushort mCPU;
private float mCPULoad;
public ushort CPU
{
get { return mCPU; }
}
public float CPULoad
{
get { return mCPULoad; }
}
public CPULoadEventArgs(ushort CurrCPU, float CurrCPULoad)
{
mCPU = CurrCPU;
mCPULoad = CurrCPULoad;
}
}
}
|