多线程工作线程状态
我使用 threadCount(= 100, 150, 255 等...) 创建线程
for (int i = 0; i < threadCount; i++)
{
Searcher src = new Searcher(i, this);
threads[i] = new Thread(new ThreadStart(src.getIpRange));
threads[i].Name = string.Format(i.ToString());
}
foreach (Thread t in threads)
{
t.Start();
}
,但我无法了解有多少线程在工作。在执行时间上。
我想控制所有线程何时完成其工作。并给我一条消息,例如“所有线程都已死亡,作业已完成......” 就像backgroundWorker的RunWorkerCompleted事件
I create my threads as
for (int i = 0; i < threadCount; i++)
{
Searcher src = new Searcher(i, this);
threads[i] = new Thread(new ThreadStart(src.getIpRange));
threads[i].Name = string.Format(i.ToString());
}
foreach (Thread t in threads)
{
t.Start();
}
with threadCount(= 100, 150, 255 etc...) but I can't learn how many threads working. on execute time.
and I want to control when all threads finishes their job. and give me a message like "All threads are dead, jobs completed..."
like backgroundWorker's RunWorkerCompleted event
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以检查线程的
ThreadState
属性。使用异步方法可能会更好。这将为您提供一个
WaitHandle
对象,您可以使用WaitHandle.WaitAll
来等待所有异步方法完成。下面是异步编程的介绍:
http://msdn.microsoft.com/ en-us/library/aa719598%28v=VS.71%29.aspx
You can check the
ThreadState
property of the Thread.Might be better to use async methods. This gives you a
WaitHandle
object, and you can useWaitHandle.WaitAll
to wait for all of your async methods to finish.Here's an intro to asynchronous programming:
http://msdn.microsoft.com/en-us/library/aa719598%28v=VS.71%29.aspx
您肯定想为此使用
Task
类或更高级别的概念,例如Parallel.ForEach
。直接使用Thread类是相当痛苦的。我最近写了一篇博客文章比较各种异步方法,按从最佳(
任务
)到最差(线程
)的顺序列出。这是一个使用
Task
的示例,演示了您想要执行的操作:You definitely want to use the
Task
class for this or a higher-level concept likeParallel.ForEach
. Using theThread
class directly is quite painful.I recently wrote a blog post comparing various asynchronous approaches, listed in order from best (
Task
) to worst (Thread
).Here's an example using
Task
, demonstrating what you wanted to do:将委托添加到 Searcher 并从主线程向其传递一个回调方法,每个线程在完成时都会调用该方法。当您启动每个线程时,将其添加到以线程的 ManagedThreadId 为键的字典中。当每个线程完成时,回调会从字典中删除该线程并检查计数是否为零。
如果您的线程数量多于您想要同时运行的数量,请将它们放入队列中,并在较旧的线程完成时将它们剥离(使用回调)。
Add a delegate to Searcher and pass it a callback method from your main thread that each thread will call when it finishes. As you launch each thread, add it to a Dictionary keyed by the thread's ManagedThreadId. When each thread finishes, the callback removes the thread from the Dictionary and checks to see if the count is zero.
If you have more threads than you want to run at one time, put them into a Queue and peel them off as older threads finish (use the callback).
首先,我必须指出,创建 100、150、255 等线程可能不是一个好主意。您最好使用 ThreadPool 或 Task 类(如果使用 .NET 4.0)。除此之外,还有两种成熟的方法可以等待所有线程完成。
加入线程。
Thread.Join
会阻塞,直到目标线程完成。使用 CountdownEvent。
A CountdownEvent< /a> 等待其内部计数达到零。如果您想使用
ThreadPool
,则此方法更适合。如果您没有使用 .NET 4.0,您可以在 Joe Albahari 的网站。First, I have to point out that creating 100, 150, 255, etc. threads is probably not a good idea. You might be better off using the
ThreadPool
orTask
class (if using .NET 4.0). Aside from that there are two well established methods for waiting until all threads complete.Join the thread.
Thread.Join
blocks until the target thread finishes.Use a CountdownEvent.
A CountdownEvent waits until its internal count reaches zero. This method is better suited if you want to use the
ThreadPool
. If you are not using .NET 4.0 you can get a really simple implementation over at Joe Albahari's website.为什么不能使用受临界区保护的单个变量来控制多个活动线程?线程函数可以修改这个变量(当然已经进入临界区)。
Why can't you use critical section protected single variable to control a number of active threads? Thread function can modify this variable (having entered critical section, of course).
确定所有线程何时完成很简单。
您能详细说明一下您的其他要求吗?
Determining when all the threads are finished is simple.
Can you elaborate on your other requirements?