質問

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?

役に立ちましたか?

解決

The thread that executes video_NewFrame is using the image when it's passed to the NewFrame event handler. Since this is running concurrently with Run, there's nothing preventing the two threads from accessing image at the same time. (This will only happen when Run dequeues the image while the NewFrame event handler is processing it, which explains why you see it only when the queue is empty or nearly empty.)

One fix might be to move the call to NewFrame before queue.Add(image); (in video_NewFrame). That would ensure that Run can't see it until the event handler is finished with it (assuming the event handler doesn't store a reference to it).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top