I got a little problem with my application.
I would like to update something on my UI every 10 seconds. I first used a DispatcherTimer for this but it will block my UI for a short time because the update method needs to load something from the web and this operation needs some time.
Now I thought about some kind of background worker and I found BackgroundTasks.
The problem with Background tasks is, as far as I understood it correctly, that they are supposed to serve as updaters even if the app is suspended. I don’t need that.
I only would like to update if my app is running not if it is suspended.
Is there a good way to solve this?
Any suggestions what to use for this?
Thanks in advance!
You need two things for it:
Timer
You can update the UI in
System.Timers.Timerwith the 10 seconds interval.Dispatcher
You need to use
Dispatcher.Invoketo change the UI without holding the main UI thread. Instead the methodProcessshould be called on a separate thread (Timermethod), other than main UI thread, and useDispatcherin it to alert main UI thread for the change.