Threading with ImageDrawings and freeze

97 views Asked by At

I have a problem creating an Image in a background-thread. I have a main-icon (16x16 pixel) that should be merged with other icons, that can be an overlay. My code for this is this:

private void GenerateConnectionIcon()
{
    var dGr = new DrawingGroup();

    var newGroupItem = new ImageDrawing(_ConnectionIcon, new Rect(0, 0, 16, 16));
    newGroupItem.Freeze(); //Here it throws the expection

    dGr.Children.Add(newGroupItem);

    foreach (var anOverlay in _ConnectionIconOverlays)
    {
        dGr.Children.Add(new ImageDrawing(anOverlay, new Rect(0, 0, 16, 16)));
    }

    dGr.Freeze();
    var finalIcon = new DrawingImage(dGr);

    finalIcon.Freeze();

    _ConnectionIconMerged = finalIcon;
}

The code quits in the third line because the calling thread is not the owner of the object. I am a little bit confused about this, because the object is created one line above. The variable _ConnectionIcon is an ImageSource and is freezed.

The exact error message is The calling thread cannot access this object because a different thread owns it.

Why I cannot freeze an object, that is created one line above?

0

There are 0 answers