Skip to content

Commit 655d55b

Browse files
committed
[#10] : Memory percent improve
1 parent 1c11351 commit 655d55b

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

src/MachineMonitor/MachineMonitor.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<Reference Include="System" />
5151
<Reference Include="System.Data" />
5252
<Reference Include="System.Drawing" />
53+
<Reference Include="System.Management" />
5354
<Reference Include="System.Windows.Forms" />
5455
<Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
5556
<HintPath>..\..\packages\MvvmLightLibs.5.4.1.1\lib\net45\System.Windows.Interactivity.dll</HintPath>
@@ -136,9 +137,7 @@
136137
<None Include="App.config" />
137138
</ItemGroup>
138139
<ItemGroup>
139-
<AdditionalFiles Include="Assets\machineicon.ico">
140-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
141-
</AdditionalFiles>
140+
<EmbeddedResource Include="Assets\machineicon.ico" />
142141
</ItemGroup>
143142
<ItemGroup>
144143
<EmbeddedResource Include="Properties\Resource.resx">

src/MachineMonitor/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@
5151
// Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
5252
// en utilisant '*', comme indiqué ci-dessous :
5353
// [assembly: AssemblyVersion("1.0.*")]
54-
[assembly: AssemblyVersion("1.0.1")]
55-
[assembly: AssemblyFileVersion("1.0.1")]
54+
[assembly: AssemblyVersion("1.0.2")]
55+
[assembly: AssemblyFileVersion("1.0.2")]

src/MachineMonitor/ViewModels/MainViewModel.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Monbsoft.MachineMonitor.Views;
66
using System;
77
using System.Diagnostics;
8+
using System.Linq;
9+
using System.Management;
810
using System.Windows.Threading;
911
using static Monbsoft.MachineMonitor.Messages.UpdatedConfigurationMessage;
1012

@@ -13,7 +15,7 @@ namespace Monbsoft.MachineMonitor.ViewModels
1315
public class MainViewModel : ViewModelBase
1416
{
1517
#region Champs
16-
private const double Giga = 1073741824d;
18+
private const double Mega = 1048576d;
1719
private readonly ConfigurationStore _configuration;
1820
private readonly PerformanceCounter _cpuCounter;
1921
private readonly PerformanceCounter _memoryCounter;
@@ -27,6 +29,7 @@ public class MainViewModel : ViewModelBase
2729
private double _networkMax;
2830
private double _ram;
2931
private MainWindow _view;
32+
private double _memoryTotal;
3033
#endregion
3134

3235
#region Constructeurs
@@ -37,8 +40,7 @@ public MainViewModel(ConfigurationStore configuration)
3740
{
3841
_configuration = configuration;
3942
_cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
40-
_memoryCounter = new PerformanceCounter("Memory", "Available Bytes");
41-
_totalRamCounter = new PerformanceCounter("Memory", "Committed Bytes");
43+
_memoryCounter = new PerformanceCounter("Memory", "Available MBytes");
4244

4345
if (configuration != null && !string.IsNullOrEmpty(configuration.Network))
4446
{
@@ -103,7 +105,27 @@ public void Initialize(MainWindow view)
103105
{
104106
_view = view;
105107
OnTransparencyChange(_configuration.Transparent);
108+
109+
// récupère le total de mémoire
110+
ManagementObjectSearcher searcher =
111+
new ManagementObjectSearcher(
112+
"SELECT * FROM Win32_ComputerSystem");
113+
114+
foreach(var mobject in searcher.Get())
115+
{
116+
_memoryTotal = ToDouble(mobject, "TotalPhysicalMemory", Mega);
117+
}
118+
106119
}
120+
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+
107129
/// <summary>
108130
/// Starts the counters.
109131
/// </summary>
@@ -113,10 +135,10 @@ public void Start()
113135
}
114136
private double CalculatePercent()
115137
{
116-
double total = _totalRamCounter.NextValue();
117138
double free = _memoryCounter.NextValue();
118-
double use = (total - free);
119-
return (use / total) * 100;
139+
double use = (_memoryTotal - free);
140+
double percent = Math.Round((use / _memoryTotal) * 100, 2);
141+
return percent;
120142
}
121143
private double GetPercentageNetwork()
122144
{

src/MachineMonitor/Views/MainWindow.xaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
AllowsTransparency="True"
99
Background="{StaticResource SystemControlBackgroundBrush}"
1010
Height="100"
11-
Icon="/MachineMonitor;component/Assets/machineicon.ico"
1211
Loaded="Window_Loaded"
1312
ResizeMode="NoResize"
1413
Title="Machine Monitor"

0 commit comments

Comments
 (0)