0

Can anyone help me with this issue...

I'm trying to create a single window with multi threading for processing images.

If i launch a process.exe for each processing operation i do not get this problem....

Here is my function:

    private ImageSource ImageApplyEffect(ImageSource imageSource, Effect effect, Size size)
    {
        DrawingVisual d = new DrawingVisual();

        DrawingContext dc = d.RenderOpen();

        dc.DrawImage(imageSource, new Rect(new Point(0, 0), size));

        d.Effect = effect;

        dc.Close();

        dc = null;

        RenderTargetBitmap frameRenderTargetBitmap2 = new RenderTargetBitmap((int)size.Width,
                                                                             (int)size.Height,
                                                                              1 / 96,
                                                                              1 / 96,
                                                                              PixelFormats.Default);
        frameRenderTargetBitmap2.Render(d);

        d = null;


        BitmapFrame frameBitmapFrame2 = BitmapFrame.Create(frameRenderTargetBitmap2);

        frameRenderTargetBitmap2 = null;

        MemoryStream mStream = new MemoryStream();

        PngBitmapEncoder encoder = new PngBitmapEncoder();

        encoder.Frames.Add(frameBitmapFrame2);

        encoder.Save(mStream);

        BitmapImage bImg = new BitmapImage();
        bImg.BeginInit();
        bImg.StreamSource = mStream;
        bImg.EndInit();

        frameBitmapFrame2 = null;


        ImageSource imgS = bImg;

        encoder = null;

        return imgS;
    }

I get this exception:

The calling thread cannot access this object because a different thread owns it!

Exception:

em System.Windows.DependencyObject.GetValue(DependencyProperty dp)
em System.Windows.Media.Effects.PixelShader.ManualUpdateResource(Channel channel, Boolean skipOnChannelCheck) em System.Windows.Media.Effects.PixelShader.UpdateResource(Channel channel, Boolean skipOnChannelCheck) em System.Windows.Media.Effects.PixelShader.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(Channel channel) em System.Windows.Media.Effects.ShaderEffect.AddRefOnChannelCore(Channel channel) em System.Windows.Media.Effects.Effect.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(Channel channel) em System.Windows.Media.Visual.UpdateEffect(Channel channel, ResourceHandle handle, VisualProxyFlags flags, Boolean isOnChannel) em System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) em System.Windows.Media.Visual.Render(RenderContext ctx, UInt32 childIndex) em System.Windows.Media.Renderer.Render(IntPtr pRenderTarget, Channel channel, Visual visual, Int32 width, Int32 height, Double dpiX, Double dpiY, Matrix worldTransform, Rect windowClip) em System.Windows.Media.Imaging.BitmapVisualManager.Render(Visual visual, Matrix worldTransform, Rect windowClip) em System.Windows.Media.Imaging.BitmapVisualManager.Render(Visual visual)

0

1 Answer 1

0

The trick here is to freeze bitmap image and pass it back to UI thread. Also, some objects need to be serialized/deserialized so that you can pass them to new thread

    public class Serializer
{
    public static Stream Serialize<T>(T t)
    {
        var memoryStream = new MemoryStream();
        var serializer = new BinaryFormatter();
        serializer.Serialize(memoryStream, t);

        return memoryStream;
    }

    public static T Deserialize<T>(Stream stream)
    {
        var formatter = new BinaryFormatter();
        stream.Seek(0, SeekOrigin.Begin);

        return (T)formatter.Deserialize(stream);
    }
}

public Task<BitmapImage> ApplyImageEffect(ImageSource imageSource, Effect effect, Size size)
{
    var imgSourceSerialized = Serializer.Serialize(imageSource);
    var effectSerialized = Serializer.Serialize(effect);

    return Task.Factory.StartNew(() =>
    {
        var imgSourceDeserialized = Serializer.Deserialize<ImageSource>(imgSourceSerialized);
        var effectDeserialized = Serializer.Deserialize<Effect>(effectSerialized);

        var drawingVisual = new DrawingVisual();
        var drawingContext = drawingVisual.RenderOpen();

        drawingContext.DrawImage(imgSourceDeserialized, new Rect(new Point(0, 0), size));
        drawingVisual.Effect = effectDeserialized;
        drawingContext.Close();

        var frameRenderTargetBitmap2 = new RenderTargetBitmap((int)size.Width,
                                                                             (int)size.Height,
                                                                              1 / 96,
                                                                              1 / 96,
                                                                              PixelFormats.Default);
        frameRenderTargetBitmap2.Render(drawingVisual);

        var frameBitmapFrame2 = BitmapFrame.Create(frameRenderTargetBitmap2);

        var memoryStream = new MemoryStream();
        var encoder = new PngBitmapEncoder();

        encoder.Frames.Add(frameBitmapFrame2);
        encoder.Save(memoryStream);

        var bitmapImage = new BitmapImage();
        bitmapImage.BeginInit();
        bitmapImage.StreamSource = memoryStream;
        bitmapImage.EndInit();

        // important!
        bitmapImage.Freeze();

        return bitmapImage;
    });
}


public MainWindow()
{
    InitializeComponent();


    Task<BitmapImage> [] tasks = new[]
    {
        ApplyImageEffect(new BitmapImage(), new BlurEffect(), new Size(20, 20)),
        ApplyImageEffect(new BitmapImage(), new BlurEffect(), new Size(20, 20))
    };

    Task.WaitAll(tasks);
}

If it doesn't work, then you need to replace Task.Factory.StartNew with "new Thread()" construct and set apartment state to STA.

There's some old code that I wrote once, that kind of demonstrates this concept:

/// <summary>
/// Creates UI element on a seperate thread and transfers it to
/// main UI thread. 
/// 
/// Usage; if you have complex UI operation that takes a lot of time, such as XPS object creation.
/// </summary>
/// <param name="constructObject">Function that creates the necessary UIElement - will be executed on new thread</param>
/// <param name="constructionCompleted">Callback to the function that receives the constructed object.</param>
public void CreateElementOnSeperateThread(Func<UIElement> constructObject, Action<UIElement> constructionCompleted)
{
    VerifyAccess();

    // save dispatchers for future usage.
    // we create new element on a seperate STA thread
    // and then basically swap UIELEMENT's Dispatcher.
    Dispatcher threadDispatcher = null;
    var currentDispatcher = Dispatcher.CurrentDispatcher;

    var ev = new AutoResetEvent(false);
    var thread = new Thread(() =>
        {
            threadDispatcher = Dispatcher.CurrentDispatcher;
            ev.Set();

            Dispatcher.Run();
        });

    thread.SetApartmentState(ApartmentState.STA);
    thread.IsBackground = true;
    thread.Start();
ev.WaitOne();

threadDispatcher.BeginInvoke(new Action(() =>
    {
        var constructedObject = constructObject();
        currentDispatcher.BeginInvoke(new Action(() =>
            {
                var fieldinfo = typeof (DispatcherObject).GetField("_dispatcher",
                                                                   BindingFlags.NonPublic |
                                                                   BindingFlags.Instance);
                if (fieldinfo != null)
                    fieldinfo.SetValue(constructedObject, currentDispatcher);

                constructionCompleted(constructedObject);
                threadDispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
            }), DispatcherPriority.Normal);
    }), DispatcherPriority.Normal);

}

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.