How to determine the number of items in a ThreadPool queue

I use ThreadPool to queue 1000 work items

While(reading in data for processing) { args = some data that has been read; ThreadPool.QueueUserWorkItem(new WaitCallback(threadRunner), args); } 

This works very well, however, since the main thread of the request queue is faster than they are being processed, memory is slowly being consumed.

I would like to do something similar to the following to disperse the queue when the queue grows

 Thread.Sleep(numberOfItemsCurrentlyQueued); 

This will increase the latency while increasing the queue.

Is there a way to find out how many items are in the queue?

+6
multithreading c # threadpool
source share
2 answers

I don’t think there is a built-in way, but you can enter a counter [static?], Which will increase / decrease; To do this, you will have to create your own method, which would wrap ThreadPool.QueueUserWorkItem () and take care of the counter.

By the way, just in case when you are using .NET 4.0, you should use TaskFactory.StartNew instead of ThreadPool.QueueUserWorkItem () - he said that he has better memory / thread control.

+2
source share

More controlled abstraction for the Producer / Consumer BlockingCollection<T> queue. The sample code shows how to use Tasks for notching and dropping the queue. The queue count is easily accessible using the Count property.

If you can, do not use Sleep to delay the production of more items. Ask the producer to wait at the Event or similarly when the queue gets too large and the consumer signals the Event when the queue lag reaches the threshold where you are comfortable, allowing you to create more items. Always try to make things event-driven - Sleep is a bit of a hunch.

+4
source share

All Articles