I need to build a process that listen in WCF for new tasks. (Async)
Every Task get Enqueue’ed (somehow).
What is the Best (Logical and Performance) way to loop the queue and Dequeue it.
I thought about:
while(true){
queue.Dequeue();
}
I assume that there are better ways to do that.
Thanks
Have a look at System.Collections.Concurrent namespace – there is thread-safe queue implementation viz. ConcurrentQueue – although, I suspect that your needs would be better served by BlockingCollection.
Blocking collection is essentially a thread-safe collection useful for producer-consumer scenario. In your case, WCF calls will act as producers that will add to the collection while the worker thread will act as consumer who would essentially take queued tasks from the collection. By using single consumer (and collection), you can ensure order of execution. If that’s not important then you may able to use multiple consumer threads. (There are also
AddAnyandTakeAnystatic overloads that will allow you to use multiple collections (multiple queues) if that is the need.)The advantage over
while(true)approach would be avoidance of tight loop that will just consume CPU cycles. Apart from having thread-safe, this would also solve issue of synchronization between queuing and de-queuing threads.EDIT:
Blocking Collection is really very simple to use. See below simple example – add task will invoked from say your WCF methods to queue up tasks while
StartConsumerwill be called during service start-up.While stopping service, one need to invoke
_tasks.CompleteAddingso that consumer thread will break.Find more examples on MSDN:
http://msdn.microsoft.com/en-us/library/dd997306.aspx
http://msdn.microsoft.com/en-us/library/dd460690.aspx
http://msdn.microsoft.com/en-us/library/dd460684.aspx