c# - How do I iterate concurrent queue repeatedly? -
i using winforms , targeting .net 4.5
i want iterate concurrent queue long has items. in application, user can add , remove items concurrent queue @ point.
sample code:
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 code below in loop? //inner loop starts here somemethod(current cq item); //move next item somemethod(the next cq item); //move next item somemethod(the next cq item); . . . //if last item reached, start top }
keep in mind application user can add or remove items queue @ time, when while loop running.
you should wrapping queue in blockingcollection
(and not accessing underlying queue directly) have thread safe queue allows wait (block) item become available. once have can use getconsumingenumerable()
if want loop through items process, or call take
explicitly each item want.
Comments
Post a Comment