From 81cb4bc8d60bba7040d1bee24a44ae05566f5f25 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Mon, 26 Oct 2015 18:31:10 -0700 Subject: set up a timer to update the UI from a background thread --- windows/WinUI/MainWindow.xaml.cs | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/windows/WinUI/MainWindow.xaml.cs b/windows/WinUI/MainWindow.xaml.cs index afe6947c..4e7638b6 100644 --- a/windows/WinUI/MainWindow.xaml.cs +++ b/windows/WinUI/MainWindow.xaml.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; +using System.Timers; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; @@ -13,6 +14,7 @@ using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using System.Windows.Threading; namespace WinUI { @@ -25,6 +27,8 @@ namespace WinUI Regex charRegex = new Regex("[0-9a-fxA-FX]"); Regex wholeStringRegex = new Regex("^[0-9a-fxA-FX]+$"); + Timer timer = new Timer(); + public MainWindow() { InitializeComponent(); @@ -33,22 +37,50 @@ namespace WinUI updateNetworks(); DataObject.AddPastingHandler(joinNetworkID, OnPaste); + + timer.Elapsed += new ElapsedEventHandler(OnUpdateTimer); + timer.Interval = 2000; + timer.Enabled = true; } private void updateStatus() { var status = handler.GetStatus(); - this.networkId.Content = status.Address; - this.versionString.Content = status.Version; - this.onlineStatus.Content = (status.Online ? "ONLINE" : "OFFLINE"); + networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + this.networkId.Content = status.Address; + })); + versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + this.versionString.Content = status.Version; + })); + onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + this.onlineStatus.Content = (status.Online ? "ONLINE" : "OFFLINE"); + })); } private void updateNetworks() { var networks = handler.GetNetworks(); - networksPage.setNetworks(networks); + networksPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + networksPage.setNetworks(networks); + })); + } + + private void updatePeers() + { + + } + + private void OnUpdateTimer(object source, ElapsedEventArgs e) + { + updateStatus(); + updateNetworks(); + updatePeers(); } private void joinButton_Click(object sender, RoutedEventArgs e) -- cgit v1.2.3