473,796 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

I have created a piece of code that uses a ThreadPool class and the
QueueUserWorkIt em 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
Nov 16 '05 #1
3 5230
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(M yCallback), 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(I AsyncResult ar);

To get the AsyncState object from inside the callback method you read
IAsyncResult.As yncState
property.
--
HTH
B\rgds
Stoitcho Goutsev (100) [C# MVP]

"SSonnenwal d" <SS*********@ya hoo.com> wrote in message
news:a0******** *************** ***@posting.goo gle.com...
I have created a piece of code that uses a ThreadPool class and the
QueueUserWorkIt em 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

Nov 16 '05 #2
> I have created a piece of code that uses a ThreadPool class and the
QueueUserWorkIt em 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
QueueUserWorkIt em() 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
Nov 16 '05 #3
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!

--
William Stacey, MVP

"SSonnenwal d" <SS*********@ya hoo.com> wrote in message
news:a0******** *************** ***@posting.goo gle.com...
I have created a piece of code that uses a ThreadPool class and the
QueueUserWorkIt em 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


Nov 16 '05 #4

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

Similar topics

10
3071
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 about 2 mins each (on a 2.8 Ghz Xeon and the server range from 2 to 8 processors) (give or take about 15 secs). I have 8 sets of calculations to do (in another app I have 121) all the same calc just different data. But the rest of the processing...
34
10818
by: Kovan Akrei | last post by:
Hi, I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program: using System; using System.Threading; using System.Collections; public class A {
1
3277
by: Mark Hoffman | last post by:
All, From what I've read, the CLR gives each App Domain a thread pool of 25 threads, and once this pool is exhausted then any new threads created with BeginInvoke will block until the pool frees up another thread. Am I right on that? I did a little test where I went into a loop and attempted to spawn 50 new worker threads with a call to BeginInvoke that used an asynchronous callback. I expected it to launch 24 threads, then block for...
1
1831
by: Novice | last post by:
Hi all, I have written some code that invokes a method a few hundred times and then it immediately finishes. I would like it to instead: 1. start the hundreds of threads (asynchronously) 2. Then block until all threads have finished executing Currently I have something like the following: -------------- MyClass myObj = new MyClass(initVar);
13
2522
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 'ThreadPool Class ') .... however I would like to ensure that all my ThreadPool threads have completed before I exit my Main function.
4
2073
by: Jeremy Holt | last post by:
Hi, In a windows.forms application I would BeginInvoke a delegate on the UI thread to collect data from a database. When the call returns to the AsyncCallback, if the Control.InvokeRequired = True, I would then have the Control.BeginInvoke(New AsyncCallback(AddressOf GetDataCallback), New Object() {ar}). How would one achieve the same thing on an asp.net page (without using a webseervice)? In the code below, because the...
6
5000
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
10
3114
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005 and .net 2.0. I started 2 threadpool threads. How do I know when they're done with their tasks? Thanks. ThreadPool.QueueUserWorkItem(new WaitCallback(PopulateContextTable)); ThreadPool.QueueUserWorkItem(new WaitCallback(PopulatAdTable)); -- Thanks.
8
2644
by: Diego F. | last post by:
Hello. I'd like to know if there's a way to know if all threads started using the threadpool are finished. How can I ensure that all threads finished? -- Regards, Diego F.
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10217
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.