views:

15

answers:

1

Is this a safe way of iterating ConcurrentQueue<T>?

while (cq.GetEnumerator().MoveNext())
{
    IIndexTask task;
    if (cq.TryDequeue(out task))
        task.Execute(service);
}
+3  A: 

Just do this instead...

IIndexTask task;
while (cq.TryDequeue(out task))
{
    task.Execute(service);
}
LukeH