WriteableBitmap exception when having multiple threads

落爺英雄遲暮 提交于 2019-12-25 17:39:34

问题


I create about 10 threads doing the same work - downloading image from the Internet. After the download is completed it will raise this callback function:

private void DownloadImageWrapper(IRestResponse response, params object[] args)
{
    byte[] imageData = response.RawBytes;
    using (Stream ms = new MemoryStream(imageData))
    {
        WriteableBitmap wbImg = PictureDecoder.DecodeJpeg(ms);

        callback.DynamicInvoke(wbImg, file);
    };
}

The exception is thrown in line with WriteableBitmap. I read that lock will help with this, but couldn't find anything about "what" I should lock there. Anyone would help?


回答1:


WriteableBitmap needs to be created on the UI thread.

You will have to keep all your photos as arrays of pixels (ints or bytes) and then create WriteableBitmaps later, after you're done, on the UI thread.



来源:https://stackoverflow.com/questions/11413132/writeablebitmap-exception-when-having-multiple-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!