Use Thread or ThreadPool

S

Simon

Hi All,

I'm currently developping something where I need to use threads.
Here's the basic scenario. I have a WebService requests which need to
launch a few threads and wait for them to complete before returning a
responses. Bascically, each thread will make a request to our old C++
server and wait for the answer before completing. Also, the WebService
request thread will wait until all the server requests thread are
completed ( or timed-out) before returning a response.
From what I've gathered, I could either use the Thread class or the
ThreadPool class to realize this. To me, it seems the ThreadPool would
be easier since I could simply tell it to start all my request and
wait for them to complete. Still, is there anything that I'm missing
here and that might make it not a good idea to use threadpool. I'm new
to C# threads and want to make sure I'm not overlooking something.

Thank you
Simon

P.S. I know that threadpool is limited to 25 threads by default and I
don't think this will create problems with what I want to do.
 
N

Nicholas Paldino [.NET/C# MVP]

Simon,

Are you looking to hold up a currently executing thread, or are you just
want to receive notification when all of the calls are complete? IIRC, the
proxies to the web services should allow you to make calls asynchronously,
so you could easily get the IAsyncResult from the async call and then wait
on all the handles that the IAsyncResult implementations expose.

If you want to be notified when all the calls are done, then you are
going to have to create a data structure which will be notified by each of
the callbacks when you make the asynchronous calls, which should then fire
it's own event when the last callback is specified.
 
S

Simon

Hi Nicholas,

I want to hold up the currently executing thread until all the others
threads have completed or timed-out. I'm just doing a demo of a new
features and want the implementation to be as easy as possible for
now. I might use asynchronous call later if needed, but for now, I'll
hold up the current thread.

My main worry here is with the WebService. I'm not sure how the
WebService works in the background to dispatch it's request, but my
guess is that it must itself use a threadpool. If I use a threadpool
in the code handling a WebService request, would I be queuing items in
the same threadpool as the WebService?

Simon
 
N

Nicholas Paldino [.NET/C# MVP]

Simon,

In this case, you don't have to use the threadpool directly, as the
proxies which you are going to use are going to use the threadpool to make
the calls to the web service.

Yes, if you are handling other tasks in the thread pool, then the web
request is going to use the same thread pool.

If you want to hold up the currently executing thread, make
asyncnrhonous calls, getting the IAsyncResult implementations, and then call
the WaitHandle property, placing the result in an array of WaitHandles.
Then pass that array to the static WaitAll method on the WaitHandle class,
and the call will return when all of the calls are complete.
 
J

Jon Skeet [C# MVP]

I want to hold up the currently executing thread until all the others
threads have completed or timed-out.

That sounds like a simple call to Thread.Join with a timeout.

My main worry here is with the WebService. I'm not sure how the
WebService works in the background to dispatch it's request, but my
guess is that it must itself use a threadpool. If I use a threadpool
in the code handling a WebService request, would I be queuing items in
the same threadpool as the WebService?

Yes, if you use the system threadpool. There are various third-party
threadpools available - I have one at http://pobox.com/~skeet/csharp/miscutil,
but it doesn't necessarily do everything you want or need.

Jon
 

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