1

In c# I'm trying to fade a Image in then out in my application. Its a image in the MainPage. At first it works great. But If I navigate to another site, lets say Settings.xaml and then back to MainPage the Dispatcher throws Invalid cross-thread access.

Anyone got an idea, this is how I'm trying to set the images opacity in my BackgroundWorker's _DoWork function:

Dispatcher.BeginInvoke(() =>
                    {
                        MyImage.Opacity = opacity;
                    });

Funny, it does not break, but put a breakpoint on it and it says everything is Invalid cross-thread access.

Shai
  • 111,146
  • 38
  • 238
  • 371
Jason94
  • 13,320
  • 37
  • 106
  • 184
  • you use the OnProgressChanged event to update the UI. See here: http://stackoverflow.com/questions/1862590/how-to-update-gui-with-backgroundworker – Ric Jan 12 '13 at 17:33
  • Just for completeness, you know that you can animate a control's `Opacity` by a `DoubleAnimation` in a `Storyboard`? – Clemens Jan 12 '13 at 18:04

2 Answers2

1

Try to implement your fading logic using Animation, not BackgroundWorker. It may help.

Have you tried this way?

Another way using Windows Phone Toolkit:

WP7 Transitions in depth | key concepts and API

WP7 Transitions in depth | custom transitions

It's old articles, but they may contain usefull information.

Alexander Balte
  • 900
  • 5
  • 11
1

Try this:

Dispatcher.Invoke(DispatcherPriority.ContextIdle, new Action(delegate()
        {
            //your code in another thread you want to invoke in UI thread
        }));

You should invoke it from the UI thread, for example from MainWindow. From my project:

//Event raised on ImageSource property changed while Backgroundwoker in MainWindow class:
    void binding_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        MyProject.ImageCropSize crop = MyProject.ImageCropSize.SourceCrop;

        this.Dispatcher.Invoke(DispatcherPriority.ContextIdle, new Action(delegate()
        {
            BitmapImage bmisource = image1.Source as BitmapImage;
            bmisource = null;//new BitmapImage();
            GC.Collect();

            BitmapImage bmi = MyProject.ImageData.ConvertFromBitmapToBitmapImage(((ImageBinding)sender).BitMap, crop);
            image1.Source = bmi;
            ((ImageBinding)sender).Clear();

        }));
    }
gleb.kudr
  • 1,518
  • 12
  • 16