I have a TextBlock on my application. I want to update it every second to show the current time.
How should I do it? The first thing I though was to create an instance of a System.Thread.Timer and update the text…
clockTimer = new Timer(o =>
{
Dispatcher.BeginInvoke(() =>
{
currentTime.Text = DateTime.Now.ToShortTimeString();
});
}, null, 0, 1000);
But is it ok to call Dispatcher every second?
If I use a binding what will be its behavior? Is it better for performance than calling Dispatcher? (consider having several calls to Dispatcher every second and several bindings)
You can use a DispatcherTimer instead.
Something like this:
There shouldn’t be harm in doing this, just don’t hang around too long in your tick event.
Also, if you do it in binding, you’re going to have to have a timer of some sort around it to update your bound property, so, though I always favor databinding over direct manipulation of the control, it shouldn’t matter either way.