wpf dispatcher/threading issue

Posted by phm on Stack Overflow See other posts from Stack Overflow or by phm
Published on 2010-03-24T09:08:23Z Indexed on 2010/03/24 9:13 UTC
Read the original article Hit count: 316

Filed under:
|
|

Hello

I have a problem in my code and I am not able to fix it at all.

private static void SetupImages(object o)
    {
        int i = (int)o;
        BitmapImage bi = GetBitmapObject(i);
        img = new System.Windows.Controls.Image();//declared as static outside

        img.Source = bi;//crash here
        img.Stretch = Stretch.Uniform;
        img.Margin = new Thickness(5, 5, 5, 5);
    }

which is called like this:

for (int i = 0; i < parameters.ListBitmaps.Count; i++)
        {
            ParameterizedThreadStart ts = new ParameterizedThreadStart(SetupImages);
            Thread t = new Thread(ts);
            t.SetApartmentState(ApartmentState.STA);
            t.Start(i);
            t.Join();
            //SetupImages(i);
            parameters.ListImageControls.Add(img);
        }

It always crashes on this line: img.Source = bi; The error is: "An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll

Additional information: The calling thread cannot access this object because a different thread owns it."

Thanks

© Stack Overflow or respective owner

Related posts about wpf

Related posts about threading