22using System . Collections . Generic ;
33using System . Threading . Tasks ;
44using System . Timers ;
5- using System . Windows . Controls ;
6- using System . Windows . Threading ;
75using MemPlus . Business . LOG ;
6+ using MemPlus . Views . Windows ;
87using Microsoft . VisualBasic . Devices ;
9- using Syncfusion . UI . Xaml . Gauges ;
108
119namespace MemPlus . Business . RAM
1210{
@@ -33,22 +31,6 @@ internal sealed class RamController
3331 /// </summary>
3432 private readonly LogController _logController ;
3533 /// <summary>
36- /// The Dispatcher object that can be used to update GUI components
37- /// </summary>
38- private readonly Dispatcher _dispatcher ;
39- /// <summary>
40- /// The SfCircularGauge object that can be used to present RAM usage statistics
41- /// </summary>
42- private readonly SfCircularGauge _gauge ;
43- /// <summary>
44- /// The Label object that can be used to show the total physical memory
45- /// </summary>
46- private readonly Label _lblTotal ;
47- /// <summary>
48- /// The Label object that can be used to show the available physical memory
49- /// </summary>
50- private readonly Label _lblAvailable ;
51- /// <summary>
5234 /// The ComputerInfo object that can be used to retrieve RAM usage statistics
5335 /// </summary>
5436 private readonly ComputerInfo _info ;
@@ -60,6 +42,10 @@ internal sealed class RamController
6042 /// An integer value representative of the percentage of RAM usage that should be reached before RAM optimisation should be called
6143 /// </summary>
6244 private double _autoOptimizeRamThreshold ;
45+ /// <summary>
46+ /// The MainWindow object that called this class
47+ /// </summary>
48+ private readonly MainWindow _mainWindow ;
6349 #endregion
6450
6551 #region Properties
@@ -100,6 +86,10 @@ internal sealed class RamController
10086 /// </summary>
10187 internal bool AutoOptimizePercentage { get ; set ; }
10288 /// <summary>
89+ /// Property displaying whether or not RAM clearing statistics should be displayed
90+ /// </summary>
91+ internal bool ShowStatistics { get ; set ; }
92+ /// <summary>
10393 /// The last time automatic RAM optimisation was called in terms of RAM percentage threshold settings
10494 /// </summary>
10595 private DateTime _lastAutoOptimizeTime ;
@@ -108,28 +98,21 @@ internal sealed class RamController
10898 /// <summary>
10999 /// Initialize a new RamController object
110100 /// </summary>
111- /// <param name="dispatcher">The Dispatcher object that can be used to update GUI components</param>
112- /// <param name="gauge">The SfCircularGauge control that can be used to present RAM usage statistics</param>
113- /// <param name="lblTotal">The Label control that can be used to display the total available memory statistics</param>
114- /// <param name="lblAvailable">The Label control that can be used to display the available memory statistics</param>
101+ /// <param name="mainWindow">The MainWindow object that called this initializer</param>
115102 /// <param name="ramUpdateTimerInterval">The interval for which RAM usage statistics should be updated</param>
116103 /// <param name="logController">The LogController object that can be used to add logs</param>
117- internal RamController ( Dispatcher dispatcher , SfCircularGauge gauge , Label lblTotal , Label lblAvailable , int ramUpdateTimerInterval , LogController logController )
104+ internal RamController ( MainWindow mainWindow , int ramUpdateTimerInterval , LogController logController )
118105 {
119106 _logController = logController ?? throw new ArgumentNullException ( nameof ( logController ) ) ;
120107 _logController . AddLog ( new ApplicationLog ( "Initializing RamController" ) ) ;
121108
122109 if ( ramUpdateTimerInterval <= 0 ) throw new ArgumentException ( "Timer interval cannot be less than or equal to zero!" ) ;
110+ _mainWindow = mainWindow ?? throw new ArgumentNullException ( nameof ( mainWindow ) ) ;
123111
124112 RamSavings = 0 ;
125113
126114 _info = new ComputerInfo ( ) ;
127115
128- _dispatcher = dispatcher ?? throw new ArgumentException ( "Dispatcher cannot be null!" ) ;
129- _gauge = gauge ?? throw new ArgumentException ( "Gauge cannot be null!" ) ;
130- _lblTotal = lblTotal ?? throw new ArgumentNullException ( nameof ( lblTotal ) ) ;
131- _lblAvailable = lblAvailable ?? throw new ArgumentNullException ( nameof ( lblAvailable ) ) ;
132-
133116 _ramOptimizer = new RamOptimizer ( _logController ) ;
134117 EmptyWorkingSets = true ;
135118 ClearStandbyCache = true ;
@@ -230,12 +213,12 @@ internal void DisableMonitor()
230213 /// </summary>
231214 private void UpdateGuiControls ( )
232215 {
233- _dispatcher . Invoke ( ( ) =>
216+ _mainWindow . Dispatcher . Invoke ( ( ) =>
234217 {
235- _gauge . Scales [ 0 ] . Pointers [ 0 ] . Value = RamUsagePercentage ;
236- _gauge . GaugeHeader = "RAM usage (" + RamUsagePercentage . ToString ( "F2" ) + "%)" ;
237- _lblTotal . Content = ( RamTotal / 1024 / 1024 / 1024 ) . ToString ( "F2" ) + " GB" ;
238- _lblAvailable . Content = ( RamUsage / 1024 / 1024 / 1024 ) . ToString ( "F2" ) + " GB" ;
218+ _mainWindow . CgRamUsage . Scales [ 0 ] . Pointers [ 0 ] . Value = RamUsagePercentage ;
219+ _mainWindow . CgRamUsage . GaugeHeader = "RAM usage (" + RamUsagePercentage . ToString ( "F2" ) + "%)" ;
220+ _mainWindow . LblTotalPhysicalMemory . Content = ( RamTotal / 1024 / 1024 / 1024 ) . ToString ( "F2" ) + " GB" ;
221+ _mainWindow . LblAvailablePhysicalMemory . Content = ( RamUsage / 1024 / 1024 / 1024 ) . ToString ( "F2" ) + " GB" ;
239222 } ) ;
240223 }
241224
@@ -272,19 +255,14 @@ await Task.Run(async () =>
272255 if ( EmptyWorkingSets )
273256 {
274257 _ramOptimizer . EmptyWorkingSetFunction ( _processExceptionList ) ;
258+ await Task . Delay ( 10000 ) ;
275259 }
276260
277261 if ( ClearFileSystemCache )
278262 {
279263 _ramOptimizer . ClearFileSystemCache ( ClearStandbyCache ) ;
280264 }
281265
282- // No need to wait if nothing happened
283- if ( EmptyWorkingSets || ClearFileSystemCache )
284- {
285- await Task . Delay ( 10000 ) ;
286- }
287-
288266 UpdateRamUsage ( ) ;
289267 UpdateGuiControls ( ) ;
290268
0 commit comments