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?
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.


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.

rxrazaAuthor Commented:

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.