3

私はWinformsを使用しており、.Net 4.5をターゲットにしています

アイテムがある限り、同時キューを反復したい。私のアプリケーションでは、ユーザーはいつでも並行キューに項目を追加および削除できます。

サンプルコード:

ConcurrentQueue<string> cq = new ConcurrentQueue<string>();

cq.Enqueue("First");
cq.Enqueue("Second");
cq.Enqueue("Third");
cq.Enqueue("Fourth");
cq.Enqueue("Fifth");

private void someMethod(string)
{
   //do stuff
}

while (!cq.IsEmpty)
{
   //how do I do the code below in a loop?

   //inner loop starts here 
   someMethod(current cq item);
   //move to the next item
   someMethod(the next cq item);
   //move to the next item
   someMethod(the next cq item);
   . 
   .
   .
   //if last item is reached, start from the top

}

while ループの実行中であっても、アプリケーション ユーザーはいつでもキューに項目を追加または削除できることに注意してください。

4

1 に答える 1

2

BlockingCollectionアイテムが利用可能になるのを待つ (ブロックする) ことができるスレッド セーフなキューを作成するには、キューを でラップする必要があります(基になるキューに直接アクセスしないでください)。GetConsumingEnumerable()処理するアイテムをループしたり、必要Takeなアイテムごとに明示的に呼び出したりする場合に使用できます。

于 2015-05-14T20:50:56.693 に答える