以下の疑似コードを参照してください
//Single or multiple Producers produce using below method
void Produce(object itemToQueue)
{
concurrentQueue.enqueue(itemToQueue);
consumerSignal.set;
}
//somewhere else we have started a consumer like this
//we have only one consumer
void StartConsumer()
{
while (!concurrentQueue.IsEmpty())
{
if (concurrentQueue.TrydeQueue(out item))
{
//long running processing of item
}
}
consumerSignal.WaitOne();
}
taskfactory で作成されたタスクと net 4 の新しいシグナリング機能を使用するために太古の昔から使用してきたこのパターンを移植するにはどうすればよいでしょうか。擬似コードは問題ありません。ご覧のとおり、私はすでに.net 4のconcurrentQueueを使用しています。タスクを使用するにはどうすればよいですか。可能であれば、新しいシグナル伝達メカニズムを使用する可能性があります。ありがとう
以下の私の問題に対する解決策は、Jon/Dan のおかげです。甘い。昔のような手動のシグナリングや while(true) または while(itemstoProcess) タイプのループはありません
//Single or multiple Producers produce using below method
void Produce(object itemToQueue)
{
blockingCollection.add(item);
}
//somewhere else we have started a consumer like this
//this supports multiple consumers !
task(StartConsuming()).Start;
void StartConsuming()
{
foreach (object item in blockingCollection.GetConsumingEnumerable())
{
//long running processing of item
}
}
cancellations are handled using cancel tokens