I have a class that implements the Begin / End Invocation template, where I originally used ThreadPool.QueueUserWorkItem () for streaming work. Work on the stream is not performed, but processing takes a little time, so the work itself does not just stop.
Now I have a side effect when someone using my class calls Begin multiple times (with a callback) to do a lot of processing, so ThreadPool.QueueUserWorkItem creates a ton of threads to process. This in itself is not bad, but there are cases when they want to refuse processing and start a new process, but they are forced to wait for the completion of their first request.
Since ThreadPool.QueueUseWorkItem () does not allow me to cancel threads, I am trying to find a better way to queue for work and possibly use the explicit FlushQueue () method in my class to allow the caller to refuse to work in my queue.
Anyone have suggestions on a thread pattern that fits my needs?
Edit: I am currently targeting environment 2.0. Currently, I think that the line of consumers / producers can work. Anyone have any thoughts on how to flush the queue?
Edit 2 Clarification of the problem: Since I use the Begin / End pattern in my class every time the caller uses Begin with callback, I create a completely new thread in the thread pool. This call does very little processing, not where I want to cancel. These are incomplete jobs in the queue that I want to stop.
The fact that ThreadPool will create 250 threads per processor by default means that if you ask ThreadPool to queue a large number of elements with QueueUserWorkItem (), you will create a huge number of parallel threads that you cannot stop.
The caller is able to push the CPU 100% not only with the job, but also with the creation of the job due to the way I queued the threads.
I was thinking, using the Producer / Consumer sample, I could put these threads in turn to allow me to reduce the number of threads that I create in order to avoid a spike in the processor creating all the parallel threads. And I could let the calling class clear all jobs in the queue when they refuse requests.
I'm currently trying to implement this, but realized that it was a good place when someone said about this code, or you wonβt be able to reset it because of this, or resetting is not the right term that you mean.