我正在使用 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 循环运行时也是如此。