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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -