I currently have a WriteableBitmap that I want to process on a non-UI thread .
I basicly want to reuse the image that is the source of my Image-control and process it in a Task.Run.
WriteableBitmap bitmap;
Task.Run(() =>
{
// Process the bitmap here
});
Unfortunately I receive the following exception because it is blocked on the UI-thread
The calling thread cannot access this object because a different thread owns it.
Note - I don't want to process the code on the UI thread!
Can someone help me please?
Objects like WriteableBitmap must be used on the thread they were created. So you have to allocate it on the other thread.
If you would like to use the processed image back on UI after processing on the worker thread, you should do one of the following:
WriteableBitmap
is DispatcherObject , only the thread that the Dispatcher was created on may access the DispatcherObject directly. To access a DispatcherObject from a thread other than the thread the DispatcherObject was created on, we need to call Invoke or BeginInvoke on the Dispatcher the DispatcherObject is associated with. And the delegate will be executed on that thread, so the render thread will block.
If you want to work with raw pixel data, WriteableBitmap
will be a good choice. We can use the CopyPixels method of the WriteableBitmap to load the pixels data to a int array, afher we modify the data in the int array
we can use the WritePixels method to update the pixels of the WriteableBitmap
. So, maybe you can try the operate the int array in the background thread.
创建要写入Task中WriteableBitmap的像素数据(数组),并在GUI线程上执行Write。
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.