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 2643
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(t hreadLock);
}
}
}
private static void WaitForAll() {
lock (threadLock) {
while (threadCount 0) {
Monitor.Wait(th readLock);
}
}
}
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.Dis pose() {
if (!disposed) {
ThreadEnded();
disposed = true;
}
}
// perhaps move the related methods / fields for start / end /
wait method here too...
}
Marc
"Marc Gravell" <ma**********@g mail.comwrote in message
news:uR******** ******@TK2MSFTN GP05.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(t hreadLock);
}
}
}
private static void WaitForAll() {
lock (threadLock) {
while (threadCount 0) {
Monitor.Wait(th readLock);
}
}
}
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******** ******@TK2MSFTN GP02.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**********@g mail.comwrote in message
news:Ok******** ******@TK2MSFTN GP05.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******** ********@TK2MSF TNGP06.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**********@g mail.comwrote in message
news:Ok******** ******@TK2MSFTN GP05.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 CustomThreadPoo l)
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 range(5): # start five threads passing i to each one
thread.start_new_thread(thrd,(i,))
|
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
long.
A Windows Service app (no probs here) that basically needs to look for work
to do from 1 to many MSMQ queues that it then needs to pass on to 1 to many
(configurable) worker threads to process. The worker thread actually runs a
few...
|
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 until ALL of them finish. I’ve
seen some code on MSDN that simply puts thread to sleep (Thread.Sleep(1000)).
While it’s acceptable in some cases, I think sleeping is dirty. 1st and
foremost, I do not know how much time a background thread is...
|
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 jobs and beeing able to
be
notified when each of these threads finished it's job.
|
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.
I know of the problems with the worker process doing two things at once from
the main thread pool (The old "Sit in a tight loop for 20 seconds and watch
all your webapps die!" issue). So I've worked around the issue by spawning
the business...
| |
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 mySub(IPAddress(i))
Next
And inside mySub, I'm initializing a new network connection (SFTP if you
|
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 the "The thread has exited with code 0 (0x12fc)." in the debugger
each time the thread seemly end, but its still there in the thread window.
GC.Collect() does not get rid of them either, something must be holding a
reference to the...
|
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.
Threading is a new concept for me to implement. Here is my problem.
I have a system that receives xml files and records their file
locations in a database. I can potentially receive thousands,
|
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 to execute a series of mathematically intensive methods and
wait for them ALL to finish before moving on to the next step. I have
a machine with lots of cores and lots of memory so threading seems
like a great solution. To be exact the...
|
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...
|
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,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |