1212using System . Windows . Input ;
1313using NETworkManager . Collections ;
1414using System . ComponentModel ;
15+ using System . Diagnostics ;
16+ using System . Windows . Threading ;
1517
1618namespace NETworkManager . ViewModels . Applications
1719{
@@ -23,6 +25,8 @@ public class PingViewModel : ViewModelBase
2325
2426 CancellationTokenSource cancellationTokenSource ;
2527
28+ DispatcherTimer dispatcherTimer = new DispatcherTimer ( ) ;
29+
2630 private bool _isLoading = true ;
2731
2832 private string _hostnameOrIPAddress ;
@@ -180,6 +184,48 @@ public int AverageTime
180184 OnPropertyChanged ( ) ;
181185 }
182186 }
187+
188+ private DateTime ? _startTime ;
189+ public DateTime ? StartTime
190+ {
191+ get { return _startTime ; }
192+ set
193+ {
194+ if ( value == _startTime )
195+ return ;
196+
197+ _startTime = value ;
198+ OnPropertyChanged ( ) ;
199+ }
200+ }
201+
202+ private TimeSpan _duration ;
203+ public TimeSpan Duration
204+ {
205+ get { return _duration ; }
206+ set
207+ {
208+ if ( value == _duration )
209+ return ;
210+
211+ _duration = value ;
212+ OnPropertyChanged ( ) ;
213+ }
214+ }
215+
216+ private DateTime ? _endTime ;
217+ public DateTime ? EndTime
218+ {
219+ get { return _endTime ; }
220+ set
221+ {
222+ if ( value == _endTime )
223+ return ;
224+
225+ _endTime = value ;
226+ OnPropertyChanged ( ) ;
227+ }
228+ }
183229 #endregion
184230
185231 #region Contructor
@@ -226,6 +272,12 @@ private async void StartPing()
226272 {
227273 IsPingRunning = true ;
228274
275+ StartTime = DateTime . Now ;
276+ Duration = new TimeSpan ( ) ;
277+ dispatcherTimer . Tick += DispatcherTimer_Tick ;
278+ dispatcherTimer . Interval = new TimeSpan ( 0 , 0 , 0 , 0 , 100 ) ;
279+ dispatcherTimer . Start ( ) ;
280+ EndTime = null ;
229281 PingResult . Clear ( ) ;
230282 PingsTransmitted = 0 ;
231283 PingsReceived = 0 ;
@@ -270,10 +322,10 @@ private async void StartPing()
270322 }
271323 }
272324 catch ( SocketException ) // This will catch DNS resolve errors
273- {
325+ {
274326 await dialogCoordinator . ShowMessageAsync ( this , Application . Current . Resources [ "String_Header_DnsError" ] as string , Application . Current . Resources [ "String_CouldNotResolveHostnameMessage" ] as string , MessageDialogStyle . Affirmative , dialogSettings ) ;
275327
276- IsPingRunning = false ;
328+ PingFinished ( ) ;
277329
278330 return ;
279331 }
@@ -302,14 +354,27 @@ private async void StartPing()
302354 ping . UserHasCanceled += Ping_UserHasCanceled ;
303355
304356 ping . SendAsync ( ipAddress , pingOptions , cancellationTokenSource . Token ) ;
305- }
357+ }
358+
359+ private void DispatcherTimer_Tick ( object sender , EventArgs e )
360+ {
361+ Duration = ( DateTime . Now - ( DateTime ) StartTime ) ;
362+ }
306363
307364 private void StopPing ( )
308365 {
309366 CancelPing = true ;
310367 cancellationTokenSource . Cancel ( ) ;
311368 }
312369
370+ private void PingFinished ( )
371+ {
372+ IsPingRunning = false ;
373+ dispatcherTimer . Stop ( ) ;
374+ EndTime = DateTime . Now ;
375+ Duration = ( DateTime ) EndTime - ( DateTime ) StartTime ;
376+ }
377+
313378 public void OnShutdown ( )
314379 {
315380 if ( IsPingRunning )
@@ -324,7 +389,7 @@ private void Ping_PingReceived(object sender, PingReceivedArgs e)
324389
325390 // Add the result to the collection
326391 PingResult . Add ( pingInfo ) ;
327-
392+
328393 // Calculate statistics
329394 PingsTransmitted ++ ;
330395
@@ -354,10 +419,10 @@ private void Ping_PingReceived(object sender, PingReceivedArgs e)
354419 PingsLost ++ ;
355420 }
356421 }
357-
422+
358423 private void Ping_PingCompleted ( object sender , System . EventArgs e )
359424 {
360- IsPingRunning = false ;
425+ PingFinished ( ) ;
361426 }
362427
363428 private async void Ping_PingException ( object sender , PingExceptionArgs e )
@@ -367,7 +432,7 @@ private async void Ping_PingException(object sender, PingExceptionArgs e)
367432
368433 string errorMessage = string . Empty ;
369434
370- switch ( w32ex . NativeErrorCode )
435+ switch ( w32ex . NativeErrorCode )
371436 {
372437 case 1231 :
373438 errorMessage = Application . Current . Resources [ "String_NetworkLocationCannotBeReached" ] as string ;
@@ -377,15 +442,16 @@ private async void Ping_PingException(object sender, PingExceptionArgs e)
377442 break ;
378443 }
379444
380- await dialogCoordinator . ShowMessageAsync ( this , Application . Current . Resources [ "String_Header_PingError" ] as string , errorMessage , MessageDialogStyle . Affirmative , dialogSettings ) ;
445+ await dialogCoordinator . ShowMessageAsync ( this , Application . Current . Resources [ "String_Header_PingError" ] as string , errorMessage , MessageDialogStyle . Affirmative , dialogSettings ) ;
381446
382- IsPingRunning = false ;
447+ PingFinished ( ) ;
383448 }
384449
385450 private void Ping_UserHasCanceled ( object sender , EventArgs e )
386451 {
387452 CancelPing = false ;
388- IsPingRunning = false ;
453+
454+ PingFinished ( ) ;
389455 }
390456 #endregion
391457 }
0 commit comments