Creating instance of BitmapImage throws exception (RPC_E_WRONG_THREAD)

3

I'm trying to create an instance of BitmapImage in a method called IsImageValid. When doing so, I get this exception:

the application called an interface that was marshalled for a different thread. (exception from hresult: 0x8001010e (rpc_e_wrong_thread))

This is my code:

public bool isImageValid(string imagePath)
    {
        try
        {
            string path = ApplicationData.Current.LocalFolder.Path;
            var image = new BitmapImage(); // Exception is thrown here!
            //BitmapImage image = new BitmapImage();
            image.ImageFailed += (s, e) => image.UriSource = new Uri(String.Empty);
            image.UriSource = new Uri(path + @"\" + imagePath);

            if (image != null)
                return true;
            return false;
        }
        catch (Exception e)
        {
            // Do stuff
        }
    }

I am new to threads and I don't understand why I can't create an instance here. What is the problem?

c#
multithreading
bitmapimage

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0