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

Way to start a number of threads and then block until they finish?

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);
for (int i = start; i <= finish; i++)
{
try
{
object iAsObj = (object) i;
ThreadPool.QueueUserWorkItem(new WaitCallback(myObj.doTask), iAsObj);
}
catch (Exception ex)
{
m_textBox.AppendText("An error occurred");
}
}
//would like to block here until all of the above threads have finished
//I was hoping ThreadPool would have a method that I could block on
-----------------------

I would like to just check to see if there are any active threads in the
thread pool and/or if there are any threads in the queue to become active.
But the ThreadPool class doesn't seem to have any way of determining if all
threads have finished executing.

The only workaround I could think of is to put a static variable in MyClass,
and increment it as I enter the doTask method and then decrement it right
before I leave the method - but this is definitely a hack. Mostly because if
I pushed this into a multi-processor environment there would be a chance that
multiple threads would attempt to change that variable at the same time -
i.e. making it a volatile variable.

Is there another way to manage my threads? Is the only other alternative to
use BeginInvoke?

Thanks,
Novice
Nov 16 '05 #1
1 1801
I believe the ThreadPool has a default maximum of 25 threads per processor (in simplistic terms) so I guess you are not really interested in the number of threads active but the number of requests remaining in the queue? Hopefully somebody will give you a simple way to detect this but if there is not then you could use the System.Threading.Interlocked class to increment/decrement your reference count.

The following might be of interst

http://msdn.microsoft.com/library/de...conMonitor.asp

Phil...



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);
for (int i = start; i <= finish; i++)
{
try
{
object iAsObj = (object) i;
ThreadPool.QueueUserWorkItem(new WaitCallback(myObj.doTask), iAsObj);
}
catch (Exception ex)
{
m_textBox.AppendText("An error occurred");
}
}
//would like to block here until all of the above threads have finished
//I was hoping ThreadPool would have a method that I could block on
-----------------------

I would like to just check to see if there are any active threads in the
thread pool and/or if there are any threads in the queue to become active.
But the ThreadPool class doesn't seem to have any way of determining if all
threads have finished executing.

The only workaround I could think of is to put a static variable in MyClass,
and increment it as I enter the doTask method and then decrement it right
before I leave the method - but this is definitely a hack. Mostly because if
I pushed this into a multi-processor environment there would be a chance that
multiple threads would attempt to change that variable at the same time -
i.e. making it a volatile variable.

Is there another way to manage my threads? Is the only other alternative to
use BeginInvoke?

Thanks,
Novice
[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #2

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

Similar topics

1
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...
4
by: Beeeeeeeeeeeeves | last post by:
HI, I am using Process.RedirectStandardOutput to read the output of a console process, quite successfully. (I am basically just doing "cmd.exe /c dir *.csproj /s /b" to list out all the .csproj...
9
by: Eric Sabine | last post by:
Can someone give me a practical example of why I would join threads? I am assuming that you would typically join a background thread with the UI thread and not a background to a background, but...
7
by: Mark B | last post by:
Hi I was wondering if someone could help me with the syntax for some VB.Net ASPX code to: - Start 20 threads - End all threads after the sooner of 10 seconds or if all of the threads...
5
by: zxo102 | last post by:
Hi, I am doing a small project using socket server and thread in python. This is first time for me to use socket and thread things. Here is my case. I have 20 socket clients. Each client send a...
3
by: Ing. Davide Piras | last post by:
Hi there, in my c# .NET 2.0 application I run few threads (from 6 to 12 it depends...) and then my GUI Thread should wait all of them have finished their task... so after create those threads i...
18
by: Jon Slaughter | last post by:
"Instead of just waiting for its time slice to expire, a thread can block each time it initiates a time-consuming activity in another thread until the activity finishes. This is better than...
2
by: Gabriel Genellina | last post by:
En Tue, 13 May 2008 06:42:13 -0300, Astan Chee <stanc@al.com.auescribió: I'm confused trying to understand your requirements too. "run a class?" Threads execute code, it's easier to think of...
5
by: eliben | last post by:
Hello, I have a small wxPython application. Today I was trying to add some RPC capability to it, so I implemented an instance of SimpleXMLRPCServer that runs in a separate thread when invoked...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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...
0
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...
0
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,...

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.