我正在使用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 循环正在运行。