So I'm building an UWP app on Windows 10 IoT Core
and a Raspberry Pi 3
. Now, I want to have a real time clock (without overloading everything) to get an accurate time (received from GPS, but nuw just from the system).
What I did now, was making an Task with infinit while loop in it, to get the time and put it onto a label... But, I can't figure out why the label doesn't get updated in that while loop, yet still it gets updated on page load. The code I'm using can be found below:
public sealed partial class Tripmaster : Page
{
public Tripmaster()
{
this.InitializeComponent();
lbl_clock.Text = DateTime.Now.ToString("HH:mm:ss");
start_clock();
btn_back.Click += Btn_back_Click;
}
private void start_clock()
{
// Init the clock and start the thread for the clock.
Task.Run(() => {
while (true)
{
lbl_clock.Text = DateTime.Now.ToString("HH:mm:ss");
Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
Task.Delay(500);
}
});
}
private void Btn_back_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(MainPage));
}
}
So the lbl_clock.Text = DateTime.Now.ToString("HH:mm:ss");
works, but never gets updated again and crashes in the while loop, any idea how I can make labels update automatically and in (near) real time?
If I just comment the lbl_clock.Text = DateTime.Now.ToString("HH:mm:ss");
out and debug it (via writing a debug line), it works, what can I do? It's really important for this project.