set up a timer to update the UI from a background thread

This commit is contained in:
Grant Limberg 2015-10-26 18:31:10 -07:00
parent 352b83252f
commit 81cb4bc8d6

View file

@ -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)