I'll test it as soon as I can.
Bye.
Jymmy097
We experienced the same issue in a multi-threading context, in a WPF application. It appears when calling
e.g. WriteableBitmap.Clear() or WriteableBitmap.DrawPolyline(int[] points, Color color)
An exception was coming from the UpdateCountByBmp-Dictionary in the 'BitmapContext.cs' (line 60).
```
private readonly static IDictionary<WriteableBitmap, int> UpdateCountByBmp = new Dictionary<WriteableBitmap, int>()
```
We changed the dictionary to a thread-safe collection and it fixed the issue for us:
```
private static readonly IDictionary<WriteableBitmap, int> UpdateCountByBmp = new System.Collections.Concurrent.ConcurrentDictionary<WriteableBitmap, int>();
```
The changes I made were to add locking (which is obviously a perf hit), otherwise it would leave the possibility of incorrectly changing the reference count.
I haven't submitted a patch because I don't think many people would be in favour of the perf-hit for the locking, but you can see a diff of my change to BitmapContext.cs here https://gist.github.com/WilkaH/6220090
It's only been tested in the context of .NET 4.0 WPF
Thanks for raising that and providing some solutions.
As you noted the lib is currently not really designed for multi-threaded scenarios. Most importantly since the WB ctor needs to be called on the UI thread anyway.
There was never a high demand for this. Actually it started with this issue report here. :) For now we won't invest in a redesign of the lib for various reasons including perf hits due to locking, etc.
If this issue report here receives more votes we might reconsider it.
Thanks!
- Rene
That seems reasonable, if only a few people need this feature they can just use one of the options mentioned here.
> Most importantly since the WB ctor needs to be called on the UI thread anyway.
The way I'm using, I'm creating the images on background threads and then freezing them so they can be used across on the UI thread. It can takes a while to build the images, and I don't want to block the UI while doing it.
We are glad to contribute!
I didn't notice considerable performance hits with the solution mentioned above (ConcurrentDictionary).
Since we need to render a great number of pictures, we used to build images in a background thread and then freeze then as well, so it was the time issue came up.
Thanks guys. :)
And yes, creating the WB on the UI thread and then simply passing the pixel buffer to fill in background thread(s) is the way to go. I use that in my apps since day one. ;)
- Rene
writeableBmp_Bkgrnd.Blit(
new Rect(0, 0, writeableBmp_Bkgrnd.PixelWidth, writeableBmp_Bkgrnd.PixelHeight), wbAddOnTop,
new Rect(0, 0, wbAddOnTop.PixelWidth, wbAddOnTop.PixelHeight),
WriteableBitmapExtensions.BlendMode.Alpha);
Thanks! writeableBmp_Bkgrnd.Blit(
new Rect(wherex, wherey, wbAddOnTop.PixelWidth, wbAddOnTop.PixelHeight),
wbAddOnTop,
new Rect(0, 0, wbAddOnTop.PixelWidth, wbAddOnTop.PixelHeight),
WriteableBitmapExtensions.BlendMode.Alpha);
-B