Threads take 5-20 seconds to clean up before finishing

jdenman

New member
Joined
Feb 10, 2006
Messages
4
Programming Experience
Beginner
My application creates threads from the ThreadPool:
VB.NET:
 ThreadPool.QueueUserWorkItem(AddressOf PullRequest)
Each thread performs it's task (check a website), reports it's progress, then is supposed to quit. I've outputted to the screen when it's done with the method PullRequest but the thread doesn't go away until 5-20 seconds after that. I've set it to only create 5 threads, but there are 20 items queued for processing, so it goes through 15 really quick. The issue is when it's done with the last 5...they just sit there for awhile, then eventually go away.

Is there some kind of cleanup I can call to clear them faster?

Also, if I want to abort (or 'gulp' pause) the threads, I have to be creating them and not using the ThreadPool correct?
 
ThreadPool manages and shares threads between work items, this saves processing time and resources. In your case 20 asynchronous calls are served by only 5 thread instances, each thread is reused for the other items in the queue and don't have to be created again. It's only natural that this management let the threads linger a while even if queue currently is empty to see if more work show up, it's for your benefit. ThreadPool is suitable not only for simplicity, but especially when many async calls finishes fast, this can really outperform creating Thread instances yourself. ThreadPool jobs can't be cancelled, but if there is opening in the job code (loops or between calls) you can put cancellation there in order for the job to stop faster.
 
Back
Top