473,218 Members | 1,673 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,218 software developers and data experts.

Re: ThreadPool question .NET 2

On Aug 22, 1:01*pm, "Brian Stoop" <b.st...@consultant-spam.free.com>
wrote:
I have several Threads that start other Threads like this:

ThreadA
{
* * for (int i=0; i <10; i++)
* * {
* * * * ThreadPool.QueueUserWorkItem(new WaitCallback(TestThread1),
(object)i);
* * }

* * // Wait for all TestThread1 to terminate before proceeding ??????

}

ThreadB
{
* * for (int i=0; i <10; i++)
* * {
* * * * ThreadPool.QueueUserWorkItem(new WaitCallback(TestThread2),
(object)i);
* * }

* * // Wait for all TestThread2 to terminate before proceeding ??????

}

--------------------------------------------------------------------------------------------------

The TestThread1 and TestThead2 methods have as a randon sleep up to 30
seconds before terminating.

How can I tell when all the ThreadA ThreadPool requests have completed ?

thanks B
A simple way could be to keep a static counter which can be updated in
the call back function "TestThread1". Make the main thread wait while
this counter is not maxed out; 10 in your case.
Aug 22 '08 #1
1 1375
On Fri, 22 Aug 2008 04:09:26 -0700, Brian Stoop
<b.*****@consultant-spam.free.comwrote:
Is there a way to get this informaton from the ThreadPool ?
Not really. Even if you could get the ThreadPool to tell you how many
busy threads there are, there could be other ThreadPool threads working as
well. There'd be no way to know that as the current count of busy
ThreadPool threads changed, that was because of your threads versus any
others.

If you follow Shree's advice, make sure you increment and decrement your
counter in a thread-safe way (e.g. see the Interlocked class) and you
don't "busy wait" on the counter (i.e. create a single AutoResetEvent that
each worker thread sets after changing the counter, and have the main
thread wait on that handle before checking the counter).

Other alternatives include giving each thread a unique WaitHandle object
(again, an AutoResetEvent would work fine), having the main thread wait on
all of them, with the worker threads setting the handle when they are
done; or, combine the two techniques with a counter that the threads
manage and a single WaitHandle that the main thread waits on, and the last
worker thread to complete would set.

IMHO the last solution is the best, as it minimizes OS resources used
(just one AutoResetEvent object) but doesn't bother the main thread until
it's guaranteed to be able to proceed (it's silly to keep waking the main
thread up just to check a counter that an already-running thread could
have checked).

Pete
Aug 22 '08 #2

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...
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...
6
by: Max Adams | last post by:
Threads and ThreadPool's If I use a ThreadPool how can I tell when a thead in the threadpool has exited? I don't want to set a global member variable I would much rather be able to act on an...
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...
7
by: =?Utf-8?B?cmJEZXZlbG9wZXI=?= | last post by:
The following is from a simple Windows application in VS2005, which has button1 and textbox1 dragged onto a form. In StartThreads(), I call ThreadPool.QueueUserWorkItem(), then call WaitOne()....
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.