473,396 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Waiting for ThreadPool

If I use standard Threading.Thread threads, I can issue a Join on the thread
variable and wait for it to complete. How can I do this on the Background
Worker Threads in a threadpool without looping. Basically, I will be
queuing an unknown number of worker threads in the pool and I need to wait
for them all to complete.

Thanks,
Mike Ober.

Apr 20 '06 #1
3 1695

"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
news:wb*****************@newsread3.news.pas.earthl ink.net...
If I use standard Threading.Thread threads, I can issue a Join on the
thread
variable and wait for it to complete. How can I do this on the Background
Worker Threads in a threadpool without looping. Basically, I will be
queuing an unknown number of worker threads in the pool and I need to wait
for them all to complete.


Pool threads never exit or they wouldn't be in the pool. Consequently you
cannot Join with them.

See WaitHandle.WaitAll

NB. It is crucial when using pool threads that you don't throw an exception
out of your delegate without signalling that it has exited (unlike Join
where you will get to know) therefore the delegate should ALWAYS be a
try...catch...finally block.

Ideally you use some sort of shared object to store the completion state,
initialize it to "software messed up" at the top of the try block, set it to
"success" at the bottom, set it to sepcific errors in the catch and signal
completion in the finally block.
Apr 20 '06 #2

Thanks.

Mike.

"Nick Hounsome" <Ne**@NickHounsome.Me.Uk> wrote in message
news:Jk*********************@fe3.news.blueyonder.c o.uk...


"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
news:wb*****************@newsread3.news.pas.earthl ink.net...
If I use standard Threading.Thread threads, I can issue a Join on the
thread
variable and wait for it to complete. How can I do this on the Background Worker Threads in a threadpool without looping. Basically, I will be
queuing an unknown number of worker threads in the pool and I need to wait for them all to complete.
Pool threads never exit or they wouldn't be in the pool. Consequently you
cannot Join with them.

See WaitHandle.WaitAll

NB. It is crucial when using pool threads that you don't throw an

exception out of your delegate without signalling that it has exited (unlike Join
where you will get to know) therefore the delegate should ALWAYS be a
try...catch...finally block.

Ideally you use some sort of shared object to store the completion state,
initialize it to "software messed up" at the top of the try block, set it to "success" at the bottom, set it to sepcific errors in the catch and signal
completion in the finally block.


Apr 20 '06 #3
In the delegate, increment an int before exit. The last delegate out closes
the door - or in this case, it does a Set(). You other thread is waiting on
event.WaitOne() similar to a Join. So one event should handle it.

try
{
// run delegate code.
}
finally
{
int c = Interlocked.Increment(ref count);
if ( c >= numToWait) // Last one out?
autoEvent.Set();
}

--
William Stacey [MVP]

"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
news:wb*****************@newsread3.news.pas.earthl ink.net...
| If I use standard Threading.Thread threads, I can issue a Join on the
thread
| variable and wait for it to complete. How can I do this on the Background
| Worker Threads in a threadpool without looping. Basically, I will be
| queuing an unknown number of worker threads in the pool and I need to wait
| for them all to complete.
|
| Thanks,
| Mike Ober.
|
|
|
Apr 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dan Battagin | last post by:
Is there a known bug with the interaction between the HttpWebRequest and the ThreadPool? I current spawn several HttpWebRequest's using BeginGetResponse, and they work for a while, using worker...
10
by: MikeE | last post by:
Hi all, What's the best way to queue up and wait for number of threads to complete. This problem was trivial in VC++ 6 but I'm finding it rather hard to solve in VB.NET. My calculations run...
5
by: Duane Pressley | last post by:
I'm looking for someone to help me make sense of the results I'm observing when using the ThreadPool class in a COM-Interop scenario. Here's the set up: 1.. A classic ASP page instantiates and calls...
1
by: doudou-shen | last post by:
I will use threadpool do some work with threadpool . but I haven't any information about it . who can help me! thank a lot
13
by: orekin | last post by:
Hi There I have been programming C# for a couple of months and am trying to master Threading. I understand that ThreadPool uses background threads (see code example in MSDN page titled...
10
by: Lenn | last post by:
Hello, I have always used a certain design pattern for multithreaded Windows app; Start new worker thread from UI thread, use events to notify UI threads when something happens, update UI...
3
by: Kevin | last post by:
Using this: http://msdn2.microsoft.com/en-us/library/3dasc8as(VS.80).aspx as an example I have a question concerning the reuse of objects. In the example 10 instances of the Fibonacci class...
5
by: =?Utf-8?B?RkxEYXZlTQ==?= | last post by:
I'm developing an application that gets data from 100 sources (via telnet connections, but you can think stock quotes from a webservice if you like). I was planning on using the thread pool (25...
3
by: UltimateBanoffee | last post by:
Hi, I'm using asp.net 2.0 and I have an understanding issue here! I don't quite understand when the available threads in the ThreadPool are ever used. The application I have running doesn't use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.