当所有线程完成时
这是我第一次真正尝试使用多线程,我想知道如何判断我的所有任务组何时完成运行:
for (int i = 0; i < taskGroups.Count(); i++) {
ThreadStart t = delegate { RunThread(taskGroups[i]); };
new Thread(t).Start();
}
if(allThreadsComplete){ //???
}
任何帮助将不胜感激
附录:
ThreadStart[] threads = new ThreadStart[taskGroups.Count()];
for (int i = 0; i < taskGroups.Count(); i++) {
threads[i] = new ThreadStart[]
threads[i] = delegate { RunThread(taskGroups[i]); };
new Thread(t).Start();
}
bool threadsComplete = false;
while(!threadsComplete){
for(int i=0;i<taskGroups.Count();i++){
if(threads[i].State == complete)
threadsComplete = true;
}
}
This is my first real attempt at using multithreading, I want to know how I can tell when all of my tasks groups are done running:
for (int i = 0; i < taskGroups.Count(); i++) {
ThreadStart t = delegate { RunThread(taskGroups[i]); };
new Thread(t).Start();
}
if(allThreadsComplete){ //???
}
Any help would be much appreciated
Addendum:
ThreadStart[] threads = new ThreadStart[taskGroups.Count()];
for (int i = 0; i < taskGroups.Count(); i++) {
threads[i] = new ThreadStart[]
threads[i] = delegate { RunThread(taskGroups[i]); };
new Thread(t).Start();
}
bool threadsComplete = false;
while(!threadsComplete){
for(int i=0;i<taskGroups.Count();i++){
if(threads[i].State == complete)
threadsComplete = true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要存储所有线程,然后调用 Thread.Join()。
像这样的事情:
You need to store all your threads, and then call Thread.Join().
Something like this:
如果您使用的是 3.5,那么您可以编写自己的 CountdownEvent,如果您使用的是 4.0,那么您可以使用内置的 CountdownEvent 执行如下操作:
latch.Wait()
将导致您的代码阻塞,直到线程全部完成。此外,您可能想稍微改变一下启动线程的方式:请注意,我将线程设置为后台:这样您的应用程序在退出时不会挂起,并且并非所有线程都已完成(即防止幽灵或守护线程)。
If you're using 3.5 then you can write your own CountdownEvent, if you're using 4.0 then you can use the built in CountdownEvent to do something like this:
The
latch.Wait()
will cause your code to block until the threads have all finished. Furthermore, you might want to change the way you start your thread a bit:Note that I'm setting the thread to background: this your application from hanging when exit and not all threads have finished (i.e. prevents ghost or daemon threads).
您可以使用 Thread.Join 确保每个单独的线程都已完成跑步。
You can use Thread.Join to make sure that each individual thread has finished running.
您可以将公共静态整数字段添加到主线程,在每个子线程中,完成后将其加一,然后在主线程中等待(循环),直到该变量等于taskGroups.Count()< /代码>。
You can add public static integer field to the main thread, in each child thread increase it by one when it's completed then in the main thread wait (in a loop) until that variable is equal to the
taskGroups.Count()
.首先考虑使用
任务
。无论如何,如果您想等待所有线程,您可以调用 Thread.Join:
请记住,您还可以传递一个超时参数,该参数仅在线程完成时间不超过指定时间时才会等待。你进来的时间。
First of all consider switching to the new asynchronous pattern using
Task
.Anyway if you want to wait for all your threads you can call
Thread.Join
:Remember that you can also pass a timeout parameter that will wait until the thread finishes only if it doesn't takes more than the time you passed in.
您可以检查每个 Thread 对象的 ThreadState 属性。
You can check the
ThreadState
property of eachThread
object.