Skip to content

Commit

Permalink
Re-use ping object
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalmers committed Jan 4, 2017
1 parent 13b36cd commit cc6540a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 35 deletions.
57 changes: 30 additions & 27 deletions Network Monitor/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
12 changes: 4 additions & 8 deletions Network Monitor/NetworkHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit cc6540a

Please sign in to comment.