Skip to content

Commit

Permalink
More changes to updating and reduce bandwidth interval
Browse files Browse the repository at this point in the history
Updates are independent from the UI and the UI only updates with the system clock.

These changes were made so the UI will instantly be updated at the same time, instead of waiting for the result, which could take a while with the ping for example.
  • Loading branch information
danielchalmers committed Jun 12, 2022
1 parent 036ca89 commit f39fd18
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 44 deletions.
8 changes: 4 additions & 4 deletions Network Monitor/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
UseLayoutRounding="True"
FontFamily="Consolas"
MouseDown="MainWindow_OnMouseDown"
PreviewMouseWheel="Window_PreviewMouseWheel"
PreviewMouseWheel="Window_PreviewMouseWheel"
Closed="Window_OnClosed">
<Window.Style>
<Style TargetType="Window">
Expand Down Expand Up @@ -145,16 +145,16 @@

<ItemsControl.ItemTemplate>
<DataTemplate>
<root:MonitorView Width="{Binding ActualWidth, ElementName=DummyMonitor}" />
<root:MonitorView Monitor="{Binding}" Width="{Binding ActualWidth, ElementName=DummyMonitor}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

<!-- Hidden dummy monitor to preserve width -->
<root:MonitorView Visibility="Hidden">
<root:MonitorView.DataContext>
<root:MonitorView.Monitor>
<monitors:DummyMonitor />
</root:MonitorView.DataContext>
</root:MonitorView.Monitor>
</root:MonitorView>
</Grid>
</Window>
4 changes: 2 additions & 2 deletions Network Monitor/MonitorView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Grid DataContext="{Binding Monitor, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Icon" />
<ColumnDefinition Width="*" SharedSizeGroup="Value" />
Expand All @@ -20,7 +20,7 @@
<Viewbox Grid.Column="1"
StretchDirection="DownOnly"
Margin="1,0,1,0">
<TextBlock Text="{Binding DisplayValue}" TextAlignment="Center" />
<TextBlock x:Name="DisplayTextBlock" TextAlignment="Center" />
</Viewbox>
</Grid>
</UserControl>
28 changes: 27 additions & 1 deletion Network Monitor/MonitorView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Windows.Controls;
using System.Windows;
using System.Windows.Controls;
using Network_Monitor.Monitors;

namespace Network_Monitor;

Expand All @@ -7,8 +9,32 @@ namespace Network_Monitor;
/// </summary>
public partial class MonitorView : UserControl
{
private readonly SystemClockTimer Timer = new();

public static readonly DependencyProperty MonitorProperty = DependencyProperty.Register(nameof(Monitor), typeof(Monitor), typeof(MonitorView), new(OnMonitorChanged));

public MonitorView()
{
InitializeComponent();
}

public Monitor Monitor
{
get => (Monitor)GetValue(MonitorProperty);
set => SetValue(MonitorProperty, value);
}

public static void OnMonitorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var view = (MonitorView)d;
var monitor = (Monitor)e.NewValue;

view.DisplayTextBlock.Text = monitor.DisplayValue;

view.Timer.SecondChanged += (_, _) =>
{
view.Dispatcher.Invoke(() => view.DisplayTextBlock.Text = monitor.DisplayValue);
};
view.Timer.Start();
}
}
6 changes: 3 additions & 3 deletions Network Monitor/Monitors/BandwidthMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public abstract class BandwidthMonitor : Monitor
private readonly char[] ByteSuffixes = new[] { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
private long _lastBytes;

protected abstract long GetTotalBytes();

protected BandwidthMonitor() : base(interval: TimeSpan.FromSeconds(1))
protected BandwidthMonitor() : base(TimeSpan.FromSeconds(2))
{
}

protected abstract long GetTotalBytes();

protected override async Task<string> GetDisplayValueAsync()
{
var bytes = await Task.Run(() => GetBytesDiffAndUpdateLast());
Expand Down
1 change: 1 addition & 0 deletions Network Monitor/Monitors/DummyMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public DummyMonitor() : base(TimeSpan.Zero)
Name = "You shouldn't be seeing this!";
Icon = 'X';
IconBrush = Brushes.Red;
DisplayValue = string.Empty.PadRight(4);
}

protected override Task<string> GetDisplayValueAsync() =>
Expand Down
2 changes: 1 addition & 1 deletion Network Monitor/Monitors/LatencyMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class LatencyMonitor : Monitor
private readonly string _host;
private readonly int _timeout;

public LatencyMonitor(string host, TimeSpan timeout) : base(interval: timeout)
public LatencyMonitor(string host, TimeSpan timeout) : base(timeout)
{
Name = "Latency";
Icon = '⟳';
Expand Down
40 changes: 7 additions & 33 deletions Network Monitor/Monitors/Monitor.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Threading;

namespace Network_Monitor.Monitors;

public abstract class Monitor : ObservableObject
public abstract class Monitor
{
private readonly SystemClockTimer _timer;
private string _displayValue = string.Empty.PadRight(4);
private readonly Timer _timer;

protected Monitor(TimeSpan interval)
{
var intervalSeconds = (int)interval.TotalSeconds;

if (intervalSeconds > 0)
{
_timer = new();
_timer.SecondChanged += async (_, _) =>
{
if (string.IsNullOrWhiteSpace(DisplayValue) || DateTime.Now.Second % intervalSeconds == 0)
await UpdateAsync();
};
_timer.Start();
}
if (interval > TimeSpan.Zero)
_timer = new Timer(async _ => await UpdateAsync(), null, TimeSpan.Zero, interval);
}

/// <summary>
Expand All @@ -41,25 +30,10 @@ protected Monitor(TimeSpan interval)
/// </summary>
public Brush IconBrush { get; protected set; }

/// <summary>
/// Interval to update in seconds.
/// </summary>
public int IntervalSeconds { get; protected set; } = 5;

/// <summary>
/// User-friendly text to show in the UI.
/// </summary>
public string DisplayValue
{
get => _displayValue;
private set
{
if (value == null || value.Length > 4)
throw new InvalidOperationException("Value can't be null or more than 4 characters.");

Set(ref _displayValue, value);
}
}
public string DisplayValue { get; protected set; }

/// <summary>
/// Gets the latest value for <see cref="DisplayValue" />.
Expand All @@ -69,7 +43,7 @@ private set
/// <summary>
/// Updates <see cref="DisplayValue"/> with the latest value.
/// </summary>
public async Task UpdateAsync()
private async Task UpdateAsync()
{
try
{
Expand Down

0 comments on commit f39fd18

Please sign in to comment.