Skip to content

Commit db12d6c

Browse files
committed
- Fixed resource monitors not updating samples if UI was frozen
! Updated background colors of resource monitors
1 parent ca13fd6 commit db12d6c

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

Codist/Display/ResourceMonitor.cs

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static class ResourceMonitor
2222
};
2323
static Meter _CpuMeter, _RamMeter, _DriveMeter;
2424
static int _IsInited;
25+
static CancellationTokenSource _CancellationTokenSource;
2526

2627
public static void Reload(DisplayOptimizations option) {
2728
if (option.HasAnyFlag(DisplayOptimizations.ResourceMonitors) == false) {
@@ -63,11 +64,14 @@ static void Stop() {
6364
}
6465

6566
static void Update(object dummy) {
66-
UpdateAsync().FireAndForget();
67+
UpdateAsync(SyncHelper.CancelAndRetainToken(ref _CancellationTokenSource)).FireAndForget();
6768
}
6869

69-
async static Task UpdateAsync() {
70-
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
70+
async static Task UpdateAsync(CancellationToken cancellationToken) {
71+
_CpuMeter?.Sample();
72+
_RamMeter?.Sample();
73+
_DriveMeter?.Sample();
74+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
7175
if (_IsInited == 0) {
7276
Init();
7377
return;
@@ -102,6 +106,7 @@ abstract class Meter : StackPanel
102106
{
103107
readonly TextBlock _Label;
104108
PerformanceCounter _Counter;
109+
float _Value;
105110

106111
protected Meter(int iconId, string tooltip) {
107112
Orientation = Orientation.Horizontal;
@@ -115,19 +120,23 @@ protected Meter(int iconId, string tooltip) {
115120

116121
protected TextBlock Label => _Label;
117122

118-
public void Update() {
123+
public void Sample() {
119124
var c = _Counter;
120-
if (c == null) {
121-
return;
125+
if (c != null) {
126+
UpdateSample(_Value = c.NextValue());
122127
}
128+
}
129+
130+
public void Update() {
123131
try {
124-
UpdateDisplay(c.NextValue());
132+
UpdateDisplay(_Value);
125133
}
126134
catch (Exception ex) {
127135
Debug.WriteLine(ex);
128136
}
129137
}
130138

139+
protected virtual void UpdateSample(float counterValue) { }
131140
protected abstract void UpdateDisplay(float counterValue);
132141
protected abstract PerformanceCounter CreateCounter();
133142

@@ -150,7 +159,7 @@ sealed class CpuMeter : Meter
150159
{
151160
const int SampleCount = 10;
152161
readonly float[] _Samples = new float[SampleCount];
153-
float _SampleSum, _LastSample;
162+
float _SampleSum, _LastCounter;
154163
int _SampleIndex;
155164

156165
public CpuMeter() : base(IconIds.Cpu, R.T_CpuUsage) {
@@ -160,25 +169,28 @@ protected override PerformanceCounter CreateCounter() {
160169
return new PerformanceCounter("Processor", "% Processor Time", "_Total");
161170
}
162171

163-
protected override void UpdateDisplay(float counterValue) {
164-
Label.Text = counterValue.ToString("0") + "%";
165-
Label.Opacity = (Math.Min(50, counterValue) + 50) / 100;
172+
protected override void UpdateSample(float counterValue) {
166173
_SampleSum -= _Samples[_SampleIndex];
167174
_Samples[_SampleIndex] = counterValue;
168175
_SampleSum += counterValue;
169176
if (++_SampleIndex == SampleCount) {
170177
_SampleIndex = 0;
171178
}
172-
counterValue = Math.Min(50, _SampleSum / SampleCount) / 50;
179+
}
180+
181+
protected override void UpdateDisplay(float counterValue) {
182+
Label.Text = counterValue.ToString("0") + "%";
183+
Label.Opacity = (Math.Min(50, counterValue) + 50) / 100;
184+
counterValue = Math.Min(50, Math.Min(counterValue, _SampleSum / SampleCount)) / 50;
173185
if (counterValue < 0.2f) {
174-
if (_LastSample >= 0.2f) {
186+
if (_LastCounter >= 0.2f) {
175187
ClearValue(BackgroundProperty);
176188
}
177189
}
178190
else {
179-
Background = Brushes.Red.Alpha(counterValue);
191+
Background = (counterValue < 0.4f ? Brushes.Yellow : counterValue < 0.6f ? Brushes.Orange : Brushes.Red).Alpha(counterValue);
180192
}
181-
_LastSample = counterValue;
193+
_LastCounter = counterValue;
182194
}
183195
}
184196

@@ -201,35 +213,40 @@ sealed class DriveMeter : Meter
201213
{
202214
const int SampleCount = 10;
203215
readonly float[] _Samples = new float[SampleCount];
204-
float _SampleSum, _LastSample;
216+
float _SampleSum, _LastCounter;
205217
int _SampleIndex;
206218

207219
public DriveMeter() : base(IconIds.Drive, R.T_DriveUsage) {
208220
}
209221

210222
protected override PerformanceCounter CreateCounter() {
211-
return new PerformanceCounter("LogicalDisk", "% Disk Time", "_Total");
223+
return new PerformanceCounter("PhysicalDisk", "% Idle Time", "_Total");
212224
}
213225

214-
protected override void UpdateDisplay(float counterValue) {
215-
Label.Text = counterValue.ToString("0") + "%";
216-
Label.Opacity = (Math.Min(50, counterValue) + 50) / 100;
226+
protected override void UpdateSample(float counterValue) {
227+
counterValue = (float)Math.Round(100 - counterValue, 0);
217228
_SampleSum -= _Samples[_SampleIndex];
218229
_Samples[_SampleIndex] = counterValue;
219230
_SampleSum += counterValue;
220231
if (++_SampleIndex == SampleCount) {
221232
_SampleIndex = 0;
222233
}
234+
}
235+
236+
protected override void UpdateDisplay(float counterValue) {
237+
counterValue = (float)Math.Round(100 - counterValue, 0);
238+
Label.Text = counterValue.ToString("0") + "%";
239+
Label.Opacity = (Math.Min(50, counterValue) + 50) / 100;
223240
counterValue = Math.Min(30, Math.Min(counterValue, _SampleSum / SampleCount)) / 30;
224241
if (counterValue < 0.2f) {
225-
if (_LastSample >= 0.2f) {
242+
if (_LastCounter >= 0.2f) {
226243
ClearValue(BackgroundProperty);
227244
}
228245
}
229246
else {
230247
Background = Brushes.Red.Alpha(counterValue);
231248
}
232-
_LastSample = counterValue;
249+
_LastCounter = counterValue;
233250
}
234251
}
235252
}

0 commit comments

Comments
 (0)