How to know when a thread in a thread pool has finished executing my workitem ?

  • Thread starter Thread starter palaga
  • Start date Start date
P

palaga

hi
I'm using QueueUserWorkItem to execute a bunch of tasks using the thread
pool. Once started, I would like to wait for all of them to finish, using
something like WaitAll. Is there a way I can know when a thread pool
workItem has finished executing ? (My only solution at this time is to use
an autoreset event that is reset before I enqueue the workItem, that is set
by my workitem when it finishes.). Is there a built-in mechanism in the
thread pool to avoid using my own autoresetEvent ?
thanks
 
palaga,

Unfortunately, no, there is not. However, it would be rather simple to
write a little wrapper delegate that will do this (instead of injecting it
into your code in every delegate).

The one thing that you have to worry about is handle management. If you
have a large number of items in the thread pool, then you can easily create
too many events which can have an impact on performance.

Check out the .NET Matters section of the October 2004 issue of MSDN
magazine, specifically, the first question. It addresses your exact
problem, as well as provides a good solution. You can find it at (watch for
line wrap):

http://msdn.microsoft.com/msdnmag/issues/04/10/NetMatters/

Hope this helps.
 
thanks, the ThreadPoolWait class seems to exactly match my needs !

Nicholas Paldino said:
palaga,

Unfortunately, no, there is not. However, it would be rather simple to
write a little wrapper delegate that will do this (instead of injecting it
into your code in every delegate).

The one thing that you have to worry about is handle management. If you
have a large number of items in the thread pool, then you can easily create
too many events which can have an impact on performance.

Check out the .NET Matters section of the October 2004 issue of MSDN
magazine, specifically, the first question. It addresses your exact
problem, as well as provides a good solution. You can find it at (watch for
line wrap):

http://msdn.microsoft.com/msdnmag/issues/04/10/NetMatters/

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

palaga said:
hi
I'm using QueueUserWorkItem to execute a bunch of tasks using the thread
pool. Once started, I would like to wait for all of them to finish, using
something like WaitAll. Is there a way I can know when a thread pool
workItem has finished executing ? (My only solution at this time is to use
an autoreset event that is reset before I enqueue the workItem, that is
set
by my workitem when it finishes.). Is there a built-in mechanism in the
thread pool to avoid using my own autoresetEvent ?
thanks
 
Back
Top