Background timer to update the interface?

I have a little problem with my application.
I would like to update something in my interface every 10 seconds. At first I used DispatcherTimer for this, but it will block my user interface for a short time, because the update method has to download something from the Internet, and this operation takes some time. Now I was thinking of some kind of background worker, and I found BackgroundTasks.
The problem with background tasks, as I understand it, is correct that they should serve as updates, even if the application is paused. I do not need it. I would only like to update if my application is running, if it is paused.

Is there a good way to solve this problem? Any suggestions what to use for this?

Thanks in advance!

+6
source share
3 answers

You need two things:

  • Timer

    You can update the System.Timers.Timer user interface every 10 seconds.

  • Control room

    You need to use Dispatcher.Invoke to change the user interface without using the main user interface thread. Instead, the Process method should be called on a separate thread ( Timer method), different from the main UI thread, and use Dispatcher in it to alert the main UI thread for change.

     Process() // method to be called after regular interval in Timer { // lengthy process, ie data fetching and processing etc. // here comes the UI update part Dispatcher.Invoke((Action)delegate() { /* update UI */ }); } 
+11
source

You need to create a thread that executes part of your code that receives and processes information from the website. Thus, your form will not fluctuate, because it will be in a different thread than the processing part.

This is the article on the code project you need to start.

+2
source

In addition, you can start a timer that has an expired event that occurs every time the timer goes through a certain time cycle.

0
source

All Articles