简体   繁体   中英

UWP bitmapimage on background thread

I have a simple question. In my UWP app I am using multiple threads and while on a background thread when i try to create a simple BitmapImage by using code: var image=new BitmapImage(); . It throws an exception

The application called an interface that was marshalled for a different thread.

this exception occurs on the very line where I try to create the image. I simply want to create this image, deal with its properties and then store it in my datalist.

Note : datalist is a simple public static property which is accesible throughout the app. thankyou

I can't see the full context from the question, so I am not sure why this exception is bubbling up, but one sure way to fix it is using CoreDispatcher.RunAsync() . The documentation says:

If you are on a worker thread and want to schedule work on the UI thread, use CoreDispatcher::RunAsync.

If you are using MVVMLight , you can also make use of it's DispatcherHelper class' CheckBeginInvokeOnUI method. It's a bit better, since it first checks which thread it is called on and if it's the UI thread, it executes the action immediately and passes it to the UI thread only if needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM