3

I have an application behaving like autorefresh. It checks emails, new videos, jokes, etc.

My client wants these single elements check in different intervals. For example emails every minute, videos every hour, etc. So there should be option for him to write down intervals by himself into textboxes and by checking appropriate checkbox start/stop refreshing.

Application is written in wpf. My question is, whether is better option to add more DispatcherTimers(DT), one for each of elements or stick with only one DT and in tick function make switch?

Also, I assume that DT's tick method runs main thread. Am I right? Is it better to run operations inside tick method on different thread if possible?

sll
  • 61,540
  • 22
  • 104
  • 156
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93

2 Answers2

4

Make it as simple as possible. As long as it is not time-consuming to check for new elements, simply use one DispatcherTimer with its own Tick handler per element category (and of course if you don't have hundreds of categories).

If the check operation is time-consuming it would block the Tick handler and thus the UI thread. Then you could use one System.Timers.Timer per element category and perform the check for new elements in the Timer's Elapsed handler (instead of Tick). Since the timers run on different threads (from the thread pool) you would have to synchronize with the UI thread:

private void VideosTimerElapsed(object sender, ElapsedEventArgs e)
{
    // check for new videos...
    Dispatcher.BeginInvoke(new Action(UpdateUI));
}

private void UpdateUI()
{
    // update UI components
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • what if I need to run two time-consuming tasks that both update the UI? Should I go for the System TImer again? Do you know any source/example that I can take a look on the synchronization with the UI thread? – gts13 Nov 13 '17 at 11:48
  • Any time-consuming task should of course not run in the UI thread. Use a Timer or ThreadPool thread or whatever. – Clemens Nov 13 '17 at 11:52
3

By default WPF Application has single thread - Main UI thread and all UI controls/windows are created and associated with it. In this case I do not see any benefits usign multiple Dispatchers and Dispatcher Timers since anyway Dispatcher Timer would delegate all messages to the associated Dispatcher's messages loop what would be a Main UI thread message loop.

But if you've created some controls in separate worker threads like

var thread = new Thread(() => 
         {  
              CustomWindow wnd = new CustomWindow();  
         };

And would post messages to this window as well - then it makes sense creating a new Dispatcher and associating with a manually created thread, so basically each Dispatcher will be associated with own thread, so relation is 1 to 1.

Regarding atick method - it would be executed on the associated with Dispatcher thread. If you've not created dispatcher associated with a manually created worker thread, so by default WPF Dispatcher associated with the main UI thread,

MSDN:

Reasons for using a DispatcherTimer opposed to a System.Timers.Timer are that the DispatcherTimer runs on the same thread as the Dispatcher and a DispatcherPriority can be set on the DispatcherTimer

sll
  • 61,540
  • 22
  • 104
  • 156
  • My english isn't good enough to fully understand your response. Can you somehow sumarize, how would you solve my task? – Ondrej Janacek Feb 15 '12 at 12:49
  • @Andrew : **1)** If you have created any threads and in this threads created new controls/windows - use separate `Dispatcher&&DispatcherTimer` for each manually created thread **2)** if you've not created any controls manually in the manually created threads - use Dispatcher.Current which should correspond to main UI thread – sll Feb 15 '12 at 13:21
  • Well, all controls are created on UI thread. Therefore you advise me to create just one DispatcherTimer, count ticks and use switch to mantain that downloading functions? – Ondrej Janacek Feb 15 '12 at 20:21