11using System ;
22using System . Threading . Tasks ;
33using System . Timers ;
4+ using System . Windows . Controls ;
45using System . Windows . Threading ;
56using Microsoft . VisualBasic . Devices ;
67using Syncfusion . UI . Xaml . Gauges ;
78
89namespace MemPlus . Classes . RAM
910{
10- internal class RamController
11+ internal sealed class RamController
1112 {
1213 private readonly Timer _ramTimer ;
14+
1315 private readonly Dispatcher _dispatcher ;
1416 private readonly SfCircularGauge _gauge ;
17+ private readonly Label _lblTotal ;
18+ private readonly Label _lblAvailable ;
19+
1520 private readonly ComputerInfo _info ;
1621
1722 internal double RamUsage { get ; private set ; }
1823 internal double RamUsagePercentage { get ; private set ; }
1924 internal double RamTotal { get ; private set ; }
2025 internal double RamSavings { get ; private set ; }
2126
22- internal RamController ( Dispatcher dispatcher , SfCircularGauge gauge , int timerInterval )
27+ internal RamController ( Dispatcher dispatcher , SfCircularGauge gauge , Label lblTotal , Label lblAvailable , int timerInterval )
2328 {
2429 if ( timerInterval <= 0 ) throw new ArgumentException ( "Timer interval cannot be less than or equal to zero!" ) ;
2530
2631 RamSavings = 0 ;
2732
2833 _info = new ComputerInfo ( ) ;
34+
2935 _dispatcher = dispatcher ?? throw new ArgumentException ( "Dispatcher cannot be null!" ) ;
3036 _gauge = gauge ?? throw new ArgumentException ( "Gauge cannot be null!" ) ;
37+ _lblTotal = lblTotal ?? throw new ArgumentNullException ( nameof ( lblTotal ) ) ;
38+ _lblAvailable = lblAvailable ?? throw new ArgumentNullException ( nameof ( lblAvailable ) ) ;
3139
3240 _ramTimer = new Timer ( ) ;
3341 _ramTimer . Elapsed += OnTimedEvent ;
@@ -53,19 +61,16 @@ private void OnTimedEvent(object source, ElapsedEventArgs e)
5361 {
5462 _gauge . Scales [ 0 ] . Pointers [ 0 ] . Value = RamUsagePercentage ;
5563 _gauge . GaugeHeader = "RAM usage (" + RamUsagePercentage . ToString ( "F2" ) + "%)" ;
64+
65+ _lblTotal . Content = ( RamTotal / 1024 / 1024 / 1024 ) . ToString ( "F2" ) + " GB" ;
66+ _lblAvailable . Content = ( ( RamTotal - RamUsage ) / 1024 / 1024 / 1024 ) . ToString ( "F2" ) + " GB" ;
5667 } ) ;
5768 }
5869
5970 internal async Task ClearMemory ( bool filesystemcache )
6071 {
61- await Task . Run ( ( ) =>
72+ await Task . Run ( async ( ) =>
6273 {
63- bool wasEnabled = _ramTimer . Enabled ;
64- if ( wasEnabled )
65- {
66- DisableMonitor ( ) ;
67- }
68-
6974 UpdateRamUsage ( ) ;
7075
7176 double oldUsage = RamUsage ;
@@ -75,15 +80,12 @@ await Task.Run(() =>
7580 //Clear file system cache
7681 MemPlus . ClearFileSystemCache ( filesystemcache ) ;
7782
83+ await Task . Delay ( 10000 ) ;
84+
7885 UpdateRamUsage ( ) ;
7986 double newUsage = RamUsage ;
8087
8188 RamSavings = oldUsage - newUsage ;
82-
83- if ( wasEnabled )
84- {
85- EnableMonitor ( ) ;
86- }
8789 } ) ;
8890 }
8991
0 commit comments