Using WriteableBitmapEx 1.0.8.0 from NuGet, I have some code where I'm creating a bunch of images in parrel. However, inside BitmapContext there's a static Dictionary used in the ctor - which leads to crashes when calling methods such as WriteableBitmapExtensions.Clear on different images concurrently.
Here's some code to reproduce the problem
```
Parallel.For(0, 1000, i =>
{
var bitmap = new WriteableBitmap(100, 100, 96, 96, PixelFormats.Gray16, null);
var converted = BitmapFactory.ConvertToPbgra32Format(bitmap);
converted.Clear(Colors.Aqua);
});
```
Comments: ** Comment from web user: wilka **
Here's some code to reproduce the problem
```
Parallel.For(0, 1000, i =>
{
var bitmap = new WriteableBitmap(100, 100, 96, 96, PixelFormats.Gray16, null);
var converted = BitmapFactory.ConvertToPbgra32Format(bitmap);
converted.Clear(Colors.Aqua);
});
```
Comments: ** Comment from web user: wilka **
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