From cc6540a158e2fa2fee7dd86a6b7defa5298cbadd Mon Sep 17 00:00:00 2001 From: Daniel Chalmers Date: Tue, 3 Jan 2017 21:35:16 -0500 Subject: [PATCH] Re-use ping object --- Network Monitor/MainWindowViewModel.cs | 57 ++++++++++++++------------ Network Monitor/NetworkHelper.cs | 12 ++---- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/Network Monitor/MainWindowViewModel.cs b/Network Monitor/MainWindowViewModel.cs index e00fbbe..e78aea9 100644 --- a/Network Monitor/MainWindowViewModel.cs +++ b/Network Monitor/MainWindowViewModel.cs @@ -87,38 +87,41 @@ private void StartLatencyReader() new Thread(() => { Thread.CurrentThread.IsBackground = true; - while (true) + using (var ping = new Ping()) { - var reply = NetworkHelper.GetLatency(); - if (reply != null) + while (true) { - var isFirstCheck = _lastLatency == -1; - - var status = reply.Status; - var latency = reply.RoundtripTime; - var downloadedBytes = NetworkHelper.GetDownloadedBytes(); - var uploadedBytes = NetworkHelper.GetUploadedBytes(); - var downloadDifference = downloadedBytes - _lastDownload; - var uploadDifference = uploadedBytes - _lastUpload; - _lastLatency = latency; - _lastDownload = downloadedBytes; - _lastUpload = uploadedBytes; - if (!isFirstCheck) + var reply = ping.GetLatency(); + if (reply != null) { - Application.Current.Dispatcher.BeginInvoke( - DispatcherPriority.Background, - new Action(() => - { - Latency = status == IPStatus.Success ? latency.ToString() : "Error"; - LatencyStatus = status; - Download = ByteHelper.BytesToString(downloadDifference); - Upload = ByteHelper.BytesToString(uploadDifference); - DownloadBytes = downloadDifference; - UploadBytes = uploadDifference; - })); + var isFirstCheck = _lastLatency == -1; + + var status = reply.Status; + var latency = reply.RoundtripTime; + var downloadedBytes = NetworkHelper.GetDownloadedBytes(); + var uploadedBytes = NetworkHelper.GetUploadedBytes(); + var downloadDifference = downloadedBytes - _lastDownload; + var uploadDifference = uploadedBytes - _lastUpload; + _lastLatency = latency; + _lastDownload = downloadedBytes; + _lastUpload = uploadedBytes; + if (!isFirstCheck) + { + Application.Current.Dispatcher.BeginInvoke( + DispatcherPriority.Background, + new Action(() => + { + Latency = status == IPStatus.Success ? latency.ToString() : "Error"; + LatencyStatus = status; + Download = ByteHelper.BytesToString(downloadDifference); + Upload = ByteHelper.BytesToString(uploadDifference); + DownloadBytes = downloadDifference; + UploadBytes = uploadDifference; + })); + } } + Thread.Sleep(Settings.Default.Interval); } - Thread.Sleep(Settings.Default.Interval); } }).Start(); } diff --git a/Network Monitor/NetworkHelper.cs b/Network Monitor/NetworkHelper.cs index d0d4705..1978349 100644 --- a/Network Monitor/NetworkHelper.cs +++ b/Network Monitor/NetworkHelper.cs @@ -6,15 +6,11 @@ namespace Network_Monitor { public static class NetworkHelper { - public static PingReply GetLatency() + public static PingReply GetLatency(this Ping ping) { - if (string.IsNullOrWhiteSpace(Settings.Default.PingHost)) - { - return null; - } - var ping = new Ping(); - var reply = ping.Send(Settings.Default.PingHost, 5000); - return reply; + return string.IsNullOrWhiteSpace(Settings.Default.PingHost) + ? null + : ping.Send(Settings.Default.PingHost, 5000); } public static long GetDownloadedBytes()