locked
Exception when creating an instance of BitmapImage from within a Task RRS feed

  • Question

  • I am using BitmapImage(uri) to fetch an jpeg from a service. If I do:

    public BitmapImage Image
    {
      get
      {
        if (null == this._imageSource)
        {
          this._imageSource = new BitmapImage(this._imageUri)
        }
        return this._imageSource;
      }
    }

    The image is retrieve as expected. However I really don't want to fetch the image from within my viewmodel so I tried:

    public BitmapImage Image
    {
       get
       {

          if (null == this._imageSource && null != this.ImageUri && null != this._imageFetcher)
          {
         Task.Factory.StartNew( () => this._imageFetcher.Invoke(this.ImageUri)).ContinueWith(t -> this.Image = t.Result);
          }  

      }
      set
      {
        this._imageSource = value;

      }

    }

    Where _imageFetcher is Func<Uri, BitmapImage) and is assigned something like:

    uri => new BitmapImage(uri)

    I received an exception coming out of the BitmapImage ctor

    Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

    Anyone have any thoughts?

    TIA

    -Mike

    Saturday, October 22, 2011 4:00 PM

Answers

  • Hi Mike,

    The RPC_E_WRONG_THREAD error indicates that the action is being made on an invalid thread.  If you call Dispatcher.Invoke to create the BitmapImage on the UI thread then this should work.

    --Rob

    Tuesday, October 25, 2011 8:10 PM
    Moderator
  • Rob,

    I really want to fetch the image and build the BitmapImage on a background thread. I'm guessing from your answer that an instance of BitmapImage can only be created on the UI thread.

    My workaround then is to fetch the image using something other than a BitmapImage and creating the BitmapImage instance when the Task returns.

    Thanks.

    -Mike

    Tuesday, October 25, 2011 9:30 PM

All replies

  • I changed the code for retrieving the images to
           protected BitmapImage FetchImage(Uri uri)
            {
                if (null == uri) return null;
    
                var bi = new BitmapImage(); <-- exception is thrown here
                 bi.UriSource = uri;
                return bi;
            }
    

    and I get a different exception:
    The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
    -Mike
    Sunday, October 23, 2011 8:06 PM
  • Hi Mike,

    The RPC_E_WRONG_THREAD error indicates that the action is being made on an invalid thread.  If you call Dispatcher.Invoke to create the BitmapImage on the UI thread then this should work.

    --Rob

    Tuesday, October 25, 2011 8:10 PM
    Moderator
  • Rob,

    I really want to fetch the image and build the BitmapImage on a background thread. I'm guessing from your answer that an instance of BitmapImage can only be created on the UI thread.

    My workaround then is to fetch the image using something other than a BitmapImage and creating the BitmapImage instance when the Task returns.

    Thanks.

    -Mike

    Tuesday, October 25, 2011 9:30 PM