Skip to content

Commit cb839bc

Browse files
committed
[#11] : Corriger le changement du réseau
1 parent 655d55b commit cb839bc

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/MachineMonitor/ViewModels/MainViewModel.cs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@ public class MainViewModel : ViewModelBase
1919
private readonly ConfigurationStore _configuration;
2020
private readonly PerformanceCounter _cpuCounter;
2121
private readonly PerformanceCounter _memoryCounter;
22-
private readonly PerformanceCounter _networkCounter;
2322
private readonly DispatcherTimer _timer;
24-
private readonly PerformanceCounter _totalRamCounter;
2523
private double _cpu;
2624
private double _disk;
2725
private PerformanceCounter _diskCounter;
26+
private double _memoryTotal;
2827
private double _network;
28+
private PerformanceCounter _networkCounter;
2929
private double _networkMax;
3030
private double _ram;
3131
private MainWindow _view;
32-
private double _memoryTotal;
3332
#endregion
3433

3534
#region Constructeurs
@@ -38,15 +37,10 @@ public class MainViewModel : ViewModelBase
3837
/// </summary>
3938
public MainViewModel(ConfigurationStore configuration)
4039
{
41-
_configuration = configuration;
40+
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
4241
_cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
4342
_memoryCounter = new PerformanceCounter("Memory", "Available MBytes");
4443

45-
if (configuration != null && !string.IsNullOrEmpty(configuration.Network))
46-
{
47-
_networkCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", configuration.Network);
48-
}
49-
5044
_timer = new DispatcherTimer();
5145
_timer.Interval = TimeSpan.FromMilliseconds(500);
5246
_timer.Tick += Timer_Tick;
@@ -55,6 +49,7 @@ public MainViewModel(ConfigurationStore configuration)
5549
Messenger.Default.Register<UpdatedConfigurationMessage>(this, HandleUpdatedConfiguration);
5650

5751
OnDiskChange();
52+
OnNetworkChange();
5853
}
5954
#endregion
6055

@@ -118,21 +113,20 @@ public void Initialize(MainWindow view)
118113

119114
}
120115

121-
private static double ToDouble(ManagementBaseObject mo, string name, double divider)
122-
{
123-
var value = mo[name];
124-
return value == null
125-
? 0
126-
: (double.TryParse(value.ToString(), out double result) ? result / divider : 0);
127-
}
128-
129116
/// <summary>
130117
/// Starts the counters.
131118
/// </summary>
132119
public void Start()
133120
{
134121
_timer.Start();
135122
}
123+
private static double ToDouble(ManagementBaseObject mo, string name, double divider)
124+
{
125+
var value = mo[name];
126+
return value == null
127+
? 0
128+
: (double.TryParse(value.ToString(), out double result) ? result / divider : 0);
129+
}
136130
private double CalculatePercent()
137131
{
138132
double free = _memoryCounter.NextValue();
@@ -163,6 +157,11 @@ private void HandleUpdatedConfiguration(UpdatedConfigurationMessage updatedMessa
163157
OnDiskChange();
164158
break;
165159
}
160+
case ChangedType.Network:
161+
{
162+
OnNetworkChange();
163+
break;
164+
}
166165
case ChangedType.Transparent:
167166
{
168167
OnTransparencyChange(_configuration.Transparent);
@@ -181,6 +180,16 @@ private void OnDiskChange()
181180
{
182181
_diskCounter = new PerformanceCounter("PhysicalDisk", "% Disk Time",_configuration.Disk);
183182
}
183+
184+
private void OnNetworkChange()
185+
{
186+
if(string.IsNullOrEmpty(_configuration.Network))
187+
{
188+
_networkCounter = null;
189+
return;
190+
}
191+
_networkCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", _configuration.Network);
192+
}
184193
private void OnTransparencyChange(bool transparent)
185194
{
186195
if (transparent)

src/MachineMonitor/Views/MainWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Height="100"
1111
Loaded="Window_Loaded"
1212
ResizeMode="NoResize"
13+
ShowInTaskbar="False"
1314
Title="Machine Monitor"
1415
Topmost="True"
1516
Width="400"

0 commit comments

Comments
 (0)