I would suggest using System.Threading.Timer
. Here some example code behind class which will update a label called timeLabel
every second with current time:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Timer timer = new Timer(TimerElapsedHandler, null, 0, 1000);
}
private void TimerElapsedHandler(object state)
{
this.Dispatcher.Invoke(() => { timeLabel.Content = DateTime.Now.ToLongTimeString(); });
}
}
You could also use some kind of BackgroundWorker/Task/whatever and have it execute something like the following in a separate thread:
while (...)
{
this.Dispatcher.Invoke(() => { timeLabel.Content = DateTime.Now.ToLongTimeString(); });
Thread.Sleep(1000);
}