99using System . Collections . Generic ;
1010using NETworkManager . Models . Network ;
1111using NETworkManager . Helpers ;
12+ using System . Windows . Threading ;
13+ using System . Diagnostics ;
1214
1315namespace NETworkManager . ViewModels . Applications
1416{
@@ -20,6 +22,9 @@ public class IPScannerViewModel : ViewModelBase
2022
2123 CancellationTokenSource cancellationTokenSource ;
2224
25+ DispatcherTimer dispatcherTimer = new DispatcherTimer ( ) ;
26+ Stopwatch stopwatch = new Stopwatch ( ) ;
27+
2328 private bool _isLoading = true ;
2429
2530 private string _ipRange ;
@@ -104,30 +109,44 @@ public bool ResolveMACAddress
104109 get { return SettingsManager . Current . IPScanner_ResolveMACAddress ; }
105110 }
106111
107- private int _progressBarMaximum ;
108- public int ProgressBarMaximum
112+ private int _ipAddressesToScan ;
113+ public int IPAddressesToScan
114+ {
115+ get { return _ipAddressesToScan ; }
116+ set
117+ {
118+ if ( value == _ipAddressesToScan )
119+ return ;
120+
121+ _ipAddressesToScan = value ;
122+ OnPropertyChanged ( ) ;
123+ }
124+ }
125+
126+ private int _ipAddressesScanned ;
127+ public int IPAddressesScanned
109128 {
110- get { return _progressBarMaximum ; }
129+ get { return _ipAddressesScanned ; }
111130 set
112131 {
113- if ( value == _progressBarMaximum )
132+ if ( value == _ipAddressesScanned )
114133 return ;
115134
116- _progressBarMaximum = value ;
135+ _ipAddressesScanned = value ;
117136 OnPropertyChanged ( ) ;
118137 }
119138 }
120139
121- private int _progressBarValue ;
122- public int ProgressBarValue
140+ private int _hostsFound ;
141+ public int HostsFound
123142 {
124- get { return _progressBarValue ; }
143+ get { return _hostsFound ; }
125144 set
126145 {
127- if ( value == _progressBarValue )
146+ if ( value == _hostsFound )
128147 return ;
129148
130- _progressBarValue = value ;
149+ _hostsFound = value ;
131150 OnPropertyChanged ( ) ;
132151 }
133152 }
@@ -145,6 +164,65 @@ public bool PreparingScan
145164 OnPropertyChanged ( ) ;
146165 }
147166 }
167+
168+ private DateTime ? _startTime ;
169+ public DateTime ? StartTime
170+ {
171+ get { return _startTime ; }
172+ set
173+ {
174+ if ( value == _startTime )
175+ return ;
176+
177+ _startTime = value ;
178+ OnPropertyChanged ( ) ;
179+ }
180+ }
181+
182+ private TimeSpan _duration ;
183+ public TimeSpan Duration
184+ {
185+ get { return _duration ; }
186+ set
187+ {
188+ if ( value == _duration )
189+ return ;
190+
191+ _duration = value ;
192+ OnPropertyChanged ( ) ;
193+ }
194+ }
195+
196+ private DateTime ? _endTime ;
197+ public DateTime ? EndTime
198+ {
199+ get { return _endTime ; }
200+ set
201+ {
202+ if ( value == _endTime )
203+ return ;
204+
205+ _endTime = value ;
206+ OnPropertyChanged ( ) ;
207+ }
208+ }
209+
210+ private bool _expandStatistics ;
211+ public bool ExpandStatistics
212+ {
213+ get { return _expandStatistics ; }
214+ set
215+ {
216+ if ( value == _expandStatistics )
217+ return ;
218+
219+ if ( ! _isLoading )
220+ SettingsManager . Current . IPScanner_ExpandStatistics = value ;
221+
222+ _expandStatistics = value ;
223+ OnPropertyChanged ( ) ;
224+ }
225+ }
148226 #endregion
149227
150228 #region Constructor, load settings
@@ -165,12 +243,12 @@ public IPScannerViewModel(IDialogCoordinator instance)
165243 _isLoading = false ;
166244 }
167245
168-
169-
170246 private void LoadSettings ( )
171247 {
172248 if ( SettingsManager . Current . IPScanner_IPRangeHistory != null )
173249 IPRangeHistory = new List < string > ( SettingsManager . Current . IPScanner_IPRangeHistory ) ;
250+
251+ ExpandStatistics = SettingsManager . Current . IPScanner_ExpandStatistics ;
174252 }
175253
176254 private void SettingsManager_PropertyChanged ( object sender , System . ComponentModel . PropertyChangedEventArgs e )
@@ -204,7 +282,16 @@ private async void StartScan()
204282 IsScanRunning = true ;
205283 PreparingScan = true ;
206284
285+ // Measure the time
286+ StartTime = DateTime . Now ;
287+ stopwatch . Start ( ) ;
288+ dispatcherTimer . Tick += DispatcherTimer_Tick ;
289+ dispatcherTimer . Interval = new TimeSpan ( 0 , 0 , 0 , 0 , 100 ) ;
290+ dispatcherTimer . Start ( ) ;
291+ EndTime = null ;
292+
207293 IPScanResult . Clear ( ) ;
294+ HostsFound = 0 ;
208295
209296 IPAddress [ ] ipAddresses ;
210297
@@ -221,8 +308,8 @@ private async void StartScan()
221308 return ;
222309 }
223310
224- ProgressBarMaximum = ipAddresses . Length ;
225- ProgressBarValue = 0 ;
311+ IPAddressesToScan = ipAddresses . Length ;
312+ IPAddressesScanned = 0 ;
226313
227314 PreparingScan = false ;
228315
@@ -254,6 +341,26 @@ private void StopScan()
254341 CancelScan = true ;
255342 cancellationTokenSource . Cancel ( ) ;
256343 }
344+
345+ private void ScanFinished ( )
346+ {
347+ IsScanRunning = false ;
348+
349+ // Stop timer and stopwatch
350+ stopwatch . Stop ( ) ;
351+ dispatcherTimer . Stop ( ) ;
352+
353+ Duration = stopwatch . Elapsed ;
354+ EndTime = DateTime . Now ;
355+
356+ stopwatch . Reset ( ) ;
357+ }
358+
359+ public void OnShutdown ( )
360+ {
361+ if ( IsScanRunning )
362+ StopScan ( ) ;
363+ }
257364 #endregion
258365
259366 #region Events
@@ -265,32 +372,32 @@ private void IpScanner_HostFound(object sender, IPScannerHostFoundArgs e)
265372 {
266373 IPScanResult . Add ( ipScannerHostInfo ) ;
267374 } ) ) ;
375+
376+ HostsFound ++ ;
268377 }
269378
270- private void IpScanner_ScanComplete ( object sender , System . EventArgs e )
379+ private void IpScanner_ScanComplete ( object sender , EventArgs e )
271380 {
272- IsScanRunning = false ;
381+ ScanFinished ( ) ;
273382 }
274383
275384 private void IpScanner_ProgressChanged ( object sender , ProgressChangedArgs e )
276385 {
277- ProgressBarValue = e . Value ;
386+ IPAddressesScanned = e . Value ;
278387 }
279388
280- private async void UserHasCanceled ( object sender , System . EventArgs e )
389+ private async void UserHasCanceled ( object sender , EventArgs e )
281390 {
282391 CancelScan = false ;
283- IsScanRunning = false ;
392+
393+ ScanFinished ( ) ;
284394
285395 await dialogCoordinator . ShowMessageAsync ( this , Application . Current . Resources [ "String_Header_CanceledByUser" ] as string , Application . Current . Resources [ "String_CanceledByUserMessage" ] as string , MessageDialogStyle . Affirmative , dialogSettings ) ;
286396 }
287- #endregion
288397
289- #region OnShutdown
290- public void OnShutdown ( )
398+ private void DispatcherTimer_Tick ( object sender , EventArgs e )
291399 {
292- if ( IsScanRunning )
293- StopScan ( ) ;
400+ Duration = stopwatch . Elapsed ;
294401 }
295402 #endregion
296403 }
0 commit comments