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?
DispatcherTimerinstead of aTimer.Dispatcherfor every thread, so you might not be using the one from the UI thread. To ensure that you are, you can use theApplication.Current.Dispatcherobject. Also see Dispatcher.CurrentDispatcher vs. Application.Current.Dispatcher for more information. However, theDispatcherTimeris the correct tool for this job as it avoids any need to use theDispatcherobject.