4

ブロッキング キューを備えた単一のプロデューサー/単一の消費者モデルを使用しています。プロデューサーが生産を終了したら、キューが空になるのを待ってから戻るようにしたいと思います。

ここで Marc Gravellが提案した BlockingQueue を実装しました。

私のモデルでは、プロデューサー (レンダラー) はイベントを使用して、ファイルがレンダリングされているとき (ワーカーがアイテムをキューに入れているとき)、およびすべてのファイルがレンダリングされたとき (終了したとき) にワーカー (プリンター) に通知します。

現在、レンダラーが完了すると、メソッドが終了し、ワーカーが強制終了されるため、印刷されていないレンダリング ファイルが 10 ~ 15 個残っています。

キューが空になるまで、たとえばすべてのファイルが印刷されるまで、終了イベント ハンドラをブロックしたいと考えています。キューが空になるまでブロックする「WaitToClose()」メソッドのようなものを追加したいと思います。

(違いを生むためにワーカースレッドを設定しIsBackground = trueますか?)

4

1 に答える 1

4

How about adding an event to the queue:

private AutoResetEvent _EmptyEvent = new AutoResetEvent(false);

Then modify the Queue to set the event when it is empty, and you can block on the event.

Thinking it through further, however, when the queue is empty, the printer will still be printing the last item.

So, then you could join (block) on the worker thread.

Simpler idea: just block on the worker thread, and have the work thread finish (exit) when the queue is empty?

于 2009-07-17T23:17:20.140 に答える