Joining on a Thread pool Thread

Posted on 2006-04-13
Last Modified: 2008-02-01
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

Question by:rxraza
    LVL 13

    Assisted Solution

    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.

    LVL 37

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



    Author Comment


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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now