views:

386

answers:

1

Hello,

I have some BitmapFrames created on a thread other than the main UI thread; Sometime after they are created (and their creator thread finishes), I am trying to use them in the main thread, as sources to some Image controls.

BUT I am getting this InvalidOperationException: The calling thread cannot access this object because a different thread owns it.

Need some help, how can I access (and use) them from the main thread?

I don't see how I can use Dispatcher.Invoke since the second thread is finished.

Thank you in advance.

+2  A: 

There are two things you have to ensure:

  1. Freeze the BitmapFrame by calling BitmapFrame.Freeze(). This turns the frame read-only, and makes it available to other threads.

  2. You may already be doing this: to let the UI thread know the frame is ready, use Dispatcher.Invoke, instead of setting the properties or calling the methods of UI objects directly.

To answer Teodor's question, freezing could fail if the BitmapFrame is still being changed. This seems to happen when you use BitmapFrame.Create(Uri). The following code appears to avoid the issue by using a decoder. In case you are creating your BitmapFrame differently, the general rule is that you have to let it finish initializing, downloading, decoding, or otherwise changing before you freeze it. Disconnect any bindings too.

Window1.xaml

<Window x:Class="BitmapFrameDemo.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <Image Name="image"/>
    </Grid>
</Window>

Window1.xaml.cs

using System;
using System.Threading;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace BitmapFrameDemo {
    public partial class Window1 : Window {
        private Thread       thread     = null;
        private Dispatcher   dispatcher = null;

        private void ThreadMain() {
            PngBitmapDecoder decoder    = new PngBitmapDecoder(
                new Uri("http://stackoverflow.com/content/img/so/logo.png"),
                BitmapCreateOptions.None,
                BitmapCacheOption.Default);
            BitmapFrame      frame      = decoder.Frames[0];
            BitmapFrame      frozen     = (BitmapFrame) frame.GetAsFrozen();
            dispatcher.Invoke(
                new Action(() => { image.Source = frozen; }),
                new object[] { });
        }

        public Window1() {
            InitializeComponent();

            dispatcher = Dispatcher.CurrentDispatcher;
            thread = new Thread(new ThreadStart(this.ThreadMain));
            thread.Start();
        }
    }
}
Oren Trutner
Thanx for the reply!I tried the first option, but i get the error: This Freezable cannot be frozen. Any help with that?
Teodor