• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 833
  • Last Modified:

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

0
rxraza
Asked:
rxraza
2 Solutions
 
devsolnsCommented:
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.

-gp
0
 
gregoryyoungCommented:
No .. sort of ...

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

example:

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++) {
           Console.WriteLine(i)
            Thread.Sleep(100);
     }
}

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.

Cheers,

Greg
0
 
rxrazaAuthor Commented:
Greg:

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

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now