Joining on a Thread pool Thread

Hi folks:

I have a thread pool thread which I am queueing like that

ThreadPool.QueueUserWorkItem(new WaitCallback(this.SomeMethod));

Now, I need to stop/kill the thread for some reason. How can I achieve this functionality ?

Also, Is  there a way I can make sure that the thread has exited. Is there a way I can join on a thread pool thread?

thanks in advance

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Im not really sure exactly what your asking.  When you call the Threadpool enqueue your passing in a callback to be called when a worker thread becomes available to process the request.  Keep in mind you can use the other variation to pass in an object to be recieved and used by the callback.  When the thread from the pool is finished processing your request it returns back to the pool and awaits another request.  Why do you need to kill the thread?  The answer to that question may point us to a solution.

No .. sort of ...

A workaround could be to make your code check a stop variable which the application sets in order to stop ..


public class SomeClass {
private bool stop = false; //note no need for locking as it is not required here

public sub SomeMethod() {
     for(int i=0;i<100000 && !this.Stop;i++) {

public void Stop() {
     this.stop = true; }

As for your Join idea ... this will not work. The reason it will not work is when the thread in the thread pool is done with your item, it will simply move on to the next (Join waits for the thread to complete).

If you are needing these behaviors then the threadpool is probably not a good match for you. You might be well off to look at other threading methods.



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rxrazaAuthor Commented:

You are right I should use some other threading mechanism or the workaround. Thanks for the help guys.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.