BlockingCollection を使用して Producer/Consumer パターンを実装しましたが、期待どおりにブロックしていないようです。
Web カメラからフレームを受信し、BlockingCollection に追加するスレッドが 1 つあります。
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
}
そして別のスレッドでは、コレクションへの参照があり、使用してフレームを処理します
public void Run() {
foreach (Bitmap bmp in queue.GetConsumingEnumerable()) {
// process bitmap
ただし、以下に示すように、InvalidOperationException をスローして、プルしているフレームが他の場所で使用されていることを通知する傾向があります。
img http://i17.photobucket.com/albums/b52/orubap/2012-03-24_020858.png
常にすぐに発生するとは限りませんが、キューが空またはほぼ空である場合にのみ発生することに気付きました (つまり、コンシューマーがプロデューサーよりも高速です)。最初に追加された画像または最後に撮った画像。なぜこれが起こっているのでしょうか?