问题
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