I am using WriteableBitmap to implement video display of a streaming video (need to re-write it up to 30 times per sec)
There are several of them on the screen and each has its own video to display.
The video also needs to be decoded so I am using worker threads (one per bitmap)to do decoding and writing into the BackBuffer and then UI thread to Lock AddDirtyRect and Unlock like so
m_WrtblBtmp.Dispatcher.Invoke(() =>
{
Debug.WriteLine("About to lock Bitmap" + m_WrtblBtmp.GetHashCode().ToString());
m_WrtblBtmp.Lock();
});
if (ReadNextVideoFrame(m_VideoDecoder, m_BackBuffer))
{
m_WrtblBtmp.Dispatcher.Invoke(() =>
{
Debug.WriteLine("About to dirty up Bitmap" + m_WrtblBtmp.GetHashCode().ToString());
m_WrtblBtmp.AddDirtyRect(new Int32Rect(0, 0, 1280, 1024));
});
}
m_WrtblBtmp.Dispatcher.Invoke(() =>
{
Debug.WriteLine("About to Unlock Bitmap" + m_WrtblBtmp.GetHashCode().ToString());
m_WrtblBtmp.Unlock();
});
ReadNextVideoFrame obtains,decodes and writes the video frame into the bitmap via the m_BackBuffer pointer (copy of the WriteableBitmap.BackBuffer) The problem I ran into is that once you have enough threads this thing deadlocks. Without Thread.Sleep I can deadlock it on 2 threads very quickly. With a bit of sleep it gets through but adding more threads will bring the problem back and I can only sleep so much before the video is slowed down to below live speed (it has to be live).
This practice is supposedly encouraged by Microsoft
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.lock(v=vs.110).aspx
Have I found a bug?
If I write to the front buffer via WritePixels there is no deadlock, but for that I need to copy the data one extra time and also tie up GUI thread for the duration of WritePixels so I would prefer to use BackBuffer if I can.
Here is the output of the Debug.WriteLine while running 2 threads:
About to lock Bitmap53653601
About to lock Bitmap31265986
About to dirty up Bitmap53653601
About to dirty up Bitmap31265986
About to Unlock Bitmap53653601
About to Unlock Bitmap31265986
... more of the same ...
About to dirty up Bitmap50546581
About to Unlock Bitmap50546581
About to dirty up Bitmap9315575
About to lock Bitmap50546581
About to Unlock Bitmap9315575
About to lock Bitmap9315575
About to dirty up Bitmap50546581
About to Unlock Bitmap50546581
About to dirty up Bitmap9315575
About to lock Bitmap50546581
... then nothing (deadlock) ...