How to tell if all the threads in a ThreadPool have completed/finished

S

SSonnenwald

I have created a piece of code that uses a ThreadPool class and the
QueueUserWorkItem method to add items to the the ThreadPool. What
would seem to be simple I just can not figure out how to tell if all
of the threads in the threadpool that I queued have completed. Does
anyone have a way???? Any help would be appreciated.

Thanks,
Scott
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi SSonnenwald,

IMHO there is now way to achieve that using ThreadPool directly.
However you can get this wroking in the way you want by calling a delegate
asynchronously.

Let's for the sake of this example assume the method you want to execute in
the thread pool has the following prototype

void Foo(int param);

1. Create a delagate object for the method you want to execute in a thread
from the thread pool.

delegate void FooDelegate(int param);

FooDelegate del = new FooDelegate(Foo);

2. Call the deleagte asynchronously and pass an AsyncCallback delegate
representing the method that will be executed when the thread finishes its
work.

del.BeginInvoke(100, new AsyncCallback(MyCallback), del);

The Foo methods will be executed in the thread from the thread pool and the
method MyCallback will be executed when the thread finishes its work.

The last parameter of the BeginInvoke is called 'AsyncState'. You can pass
any object you want and this object will be passed in turn to the MyCallback
method. Usually you pass the delegate object which BeginInvoke method you
called in order to call EndInvoke when the works done.

AsyncCallback delegate is declared as

public delegate void AsyncCallback(IAsyncResult ar);

To get the AsyncState object from inside the callback method you read
IAsyncResult.AsyncState
property.
 
D

David Sworder

I have created a piece of code that uses a ThreadPool class and the
QueueUserWorkItem method to add items to the the ThreadPool. What
would seem to be simple I just can not figure out how to tell if all
of the threads in the threadpool that I queued have completed. Does
anyone have a way???? Any help would be appreciated.

I was in a similar situation recently. I just defined a counter and
incremented the counter as I placed items in the pool and then decremented
the counter as each threadpool callback completed. When the counter reached
zero I knew everything was done.

I've come to the conclusion though that it's usually not the best idea
to stash tons of items in the thread pool (either directly by
QueueUserWorkItem() or via an async delegate). The thing is, once you've
stashed an item in there, there's no way to get it out or to re-order the
items.

David
 
W

William Stacey [MVP]

If you don't find your threadpool answer, I would just create a worker
thread class. Then create one or more instances of it and start them using
a shared sync'd queue that you filled. They all get data from your sync'd
queue. When the queue is empty, that thread stops. You can wait on all
threads to finish, then you know they are done. Others ways to do this,
just depends on what your trying to do. Cheers!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top