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. 8 2621
Your own pooled threads would have to let you know somehow... perhaps
a synchronised counter? and if your threads Pulse when decrementing
you should be able to simulate a Join (to all):
private static int threadCount;
private static readonly object threadLock = new object();
private static void ThreadStarted() {
lock (threadLock) {
threadCount++;
}
}
private static void ThreadEnded() {
lock (threadLock) {
threadCount--;
if(threadCount<=0) {
Monitor.Pulse(threadLock);
}
}
}
private static void WaitForAll() {
lock (threadLock) {
while (threadCount 0) {
Monitor.Wait(threadLock);
}
}
}
private void ThreadMethod() {
ThreadStarted();
try {
// do somthing
} finally {
ThreadEnded();
}
}
or more conveniently... (note I have not either approach myself...
just thinking aloud...)
private static void ThreadMethod() {
using (new ThreadWatcher()) {
// do something
}
}
private sealed class ThreadWatcher : IDisposable {
private bool disposed;
public ThreadWatcher() {
ThreadStarted();
}
void IDisposable.Dispose() {
if (!disposed) {
ThreadEnded();
disposed = true;
}
}
// perhaps move the related methods / fields for start / end /
wait method here too...
}
Marc
"Marc Gravell" <ma**********@gmail.comwrote in message
news:uR**************@TK2MSFTNGP05.phx.gbl...
Your own pooled threads would have to let you know somehow...
perhaps a synchronised counter? and if your threads Pulse when
decrementing you should be able to simulate a Join (to all):
private static int threadCount;
private static readonly object threadLock = new object();
private static void ThreadStarted() {
lock (threadLock) {
threadCount++;
}
}
private static void ThreadEnded() {
lock (threadLock) {
threadCount--;
if(threadCount<=0) {
Monitor.Pulse(threadLock);
}
}
}
private static void WaitForAll() {
lock (threadLock) {
while (threadCount 0) {
Monitor.Wait(threadLock);
}
}
}
private void ThreadMethod() {
ThreadStarted();
try {
// do somthing
} finally {
ThreadEnded();
}
}
What I'm looking for is a way of blocking the main thread until all threads
in ThreadPool are finished.
Is that possible with ThreadPool object?
--
Regards,
Diego F.
"Diego F." <di********@msn.comwrote in message
news:uU**************@TK2MSFTNGP02.phx.gbl...
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.
*all* threads? Or those that you started?
Since other code could bne using the ThreadPool, I wouldn't recommend
the first and I doubt that this is available. It seems very risky,
certainly; you should only be interested in threads that you own
(pooled or otherwise). For the second, see the WaitForAll() method
that I posted; it does exactly that - just don't call it on a pool
thread!
Marc
If I understand you, you suggest not using the ThreadPool, and starting my
own threads instead. Is that rigth?
--
Regards,
Diego F.
"Marc Gravell" <ma**********@gmail.comwrote in message
news:Ok**************@TK2MSFTNGP05.phx.gbl...
*all* threads? Or those that you started?
Since other code could bne using the ThreadPool, I wouldn't recommend the
first and I doubt that this is available. It seems very risky, certainly;
you should only be interested in threads that you own (pooled or
otherwise). For the second, see the WaitForAll() method that I posted; it
does exactly that - just don't call it on a pool thread!
Marc
The problem with that is if I have to start many threads.
--
Regards,
Diego F.
"Diego F." <di********@msn.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
If I understand you, you suggest not using the ThreadPool, and starting my
own threads instead. Is that rigth?
--
Regards,
Diego F.
"Marc Gravell" <ma**********@gmail.comwrote in message
news:Ok**************@TK2MSFTNGP05.phx.gbl...
>*all* threads? Or those that you started?
Since other code could bne using the ThreadPool, I wouldn't recommend the first and I doubt that this is available. It seems very risky, certainly; you should only be interested in threads that you own (pooled or otherwise). For the second, see the WaitForAll() method that I posted; it does exactly that - just don't call it on a pool thread!
Marc
I never mentioned anything about creating threads... all I am saying
is that your worker methods (be they thread-pool or otherwise) could
notify *something* to indicate that they are complete. I have just
tested it, and it "works OK for small values of works" - there is a
glitch that you can exit too soon if the pool item hasn't started yet
(and thus the counter is 0).
Another approach is to use a dedicated pool; I believe Jon has some
template code in his box of tricks: http://www.yoda.arachsys.com/csharp/miscutil/
(see CustomThreadPool)
This should allow you to track when it is empty... or allow simple
customisation
Marc
On Jun 7, 8:07 am, "Diego F." <diego_f...@msn.comwrote:
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.
Take a look at how to use the WaitAll WaitHandle method: http://msdn2.microsoft.com/en-us/library/z6w25xa6.aspx
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Valkyrie |
last post by:
Refering to the following codes I found, there is nothing displayed in the
console, may I ask why?
def thrd(param): # the thread worker function
print "Received",param
import thread
for i in...
|
by: Stephen Barrett |
last post by:
After reading through the many many many posts on threading, I still don't
quite understand how to accomplish a task.
Here is an overview of the app I am coding. I am sorry if it is a little...
|
by: Mike |
last post by:
Hi! I have several methods of different importance that I’d like to call
asynchronously from Main(). I am going to span these threads but will have to
implement some kind of a mechanism to wait...
|
by: objectref |
last post by:
hi to all folks,
i want to create the following senario:
i have a T1 (Thread 1) that acts like http server that receives incoming
requests.
From that T1, i want to spawn from T1 to Tn thread...
|
by: John |
last post by:
I've got some reasonably complex business logic in my C# code, in a class
called by a ASP.NET page. This takes around 3-4 seconds to execute. It's not
dependent on SQL calls or anything like that....
|
by: OpticTygre |
last post by:
Heheh...I've got lots of questions today.
If I have a loop that calls the same subroutine several times (that
subroutine may be processor and network intensive):
For i = 1 to 100
Call...
|
by: AdrianJMartin |
last post by:
Hi all,
I have a need for a STA thread from asp.net. I can create the thread and it
runs fine. But when it is finished, the thread still 'hangs' arround.
Visible only to the debugger.....
I get...
|
by: frankiespark |
last post by:
Hello all,
I was perusing the internet for information on threading when I came
across this group. Since there seems to be a lot of good ideas and
useful info I thought I'd pose a question.
...
|
by: keeling |
last post by:
Greetings all,
It is my understanding that polling is very bad (I could be wrong,
this is just my understanding). But I have a problem that I don't know
how to solve without polling.
I need...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |