I've implemented a Producer/Consumer pattern using a BlockingCollection however it doesn't seem to be blocking as I expect.
I have one thread receiving Frames from a webcam and adding them to the BlockingCollection
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) {
image = (Bitmap)eventArgs.Frame.Clone();
queue.Add(image);
if (NewFrame != null)
NewFrame(this, new NewFrameEventArgs(image)); //invoke the event for display
}
And in another thread I have a reference to the collection and process the Frames using
public void Run() {
foreach (Bitmap bmp in queue.GetConsumingEnumerable()) {
// process bitmap
However, as you can see below it tends to throw an InvalidOperationException telling me the Frame I'm pulling is in use elsewhere.
img http://i17.photobucket.com/albums/b52/orubap/2012-03-24_020858.png
It doesn't always happen straight away but I've noticed it only occurs when the queue is empty or near empty (ie. the consumer is faster than the producer) so I'm guessing it's something to do with the first image added or the last image taken. Any ideas why this might be happening?
BlockingCollection
has no knowledge of other things you might be doing, it won't help you with this. – svick