3

Within the App I display banners that rotate as time progresses or when user clicks next/previous.

1) I need to update an UI element within Timer.Elapsed method. From what I found, it seems that timer ticks are executed on their own thread and element's dispatcher should be use to update the UI.

( (Image)BannerPanel.Content ).Dispatcher.Invoke( new Action( () => {
    ( (Image)BannerPanel.Content ).Source = GetImage( new Uri( banner.Uri, UriKind.Absolute ) );
} ) );

but that throws an InvalidOperationException.

2) The banner controller provides Next and Previous methods, which, when called display next/previous banner respectively. As DisplayBanner method tries to display banner and when the file is not found it tries to re-download it using WebClient's AsyncFileDownload and on DownloadComplete it displays the image. Am I correct to assume that both, Elapsed fired by the timer and manual call of Next/Previous can occure at the same time, both being run on their own thread ( Previous/Next on UI thread and Elapsed on Timer thread ), possibly causing instability?

2
  • Just use a DispatcherTimer instead of a Timer. Commented Sep 4, 2014 at 15:11
  • There is a Dispatcher for every thread, so you might not be using the one from the UI thread. To ensure that you are, you can use the Application.Current.Dispatcher object. Also see Dispatcher.CurrentDispatcher vs. Application.Current.Dispatcher for more information. However, the DispatcherTimer is the correct tool for this job as it avoids any need to use the Dispatcher object. Commented Sep 4, 2014 at 15:34

2 Answers 2

3

You should look into using the DispatcherTimer class instead of a regular Timer. It's designed to be used with WPF UI for just these types of uses because it runs on the Dispatcher thread.

More info: System.Windows.Threading.DispatcherTimer

Sign up to request clarification or add additional context in comments.

2 Comments

Alright, yet do you know what's the issue with the dispatcher call I was trying to do? Just for reference.
@pikausp, see my comment on your question for that answer. This is currently the best answer in my opinion.
1

Try using Application instead,

Application.Current.Dispatcher.Invoke(new Action(() => 
{
    ((Image)BannerPanel.Content).Source = GetImage(new Uri(banner.Uri, UriKind.Absolute));
});

2 Comments

It doesn't crash anymore, but I switched to using DispatcherTimer as recommended by others.
@pikausp, yeah, DispatcherTimer is more topical when it comes to your situation. Though, this is another alternative. I would recommend using Application.Current whenever you want to do an Invoke or BeginInvoke.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.