当所有线程完成时

发布于 2024-10-21 20:37:45 字数 713 浏览 3 评论 0原文

这是我第一次真正尝试使用多线程,我想知道如何判断我的所有任务组何时完成运行:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一紙繁鸢 2024-10-28 20:37:45

您需要存储所有线程,然后调用 Thread.Join()。

像这样的事情:

List<Thread> threads = new List<Thread>();
for (int i = 0; i < taskGroups.Count(); i++) {
   int temp = i; //This fixes the issue with i being shared
   Thread thread = new Thread(() => RunThread(taskGroups[temp]));
   threads.Add(thread);
   thread.Start();
}

foreach (var thread in threads) {
    thread.Join();
}

You need to store all your threads, and then call Thread.Join().

Something like this:

List<Thread> threads = new List<Thread>();
for (int i = 0; i < taskGroups.Count(); i++) {
   int temp = i; //This fixes the issue with i being shared
   Thread thread = new Thread(() => RunThread(taskGroups[temp]));
   threads.Add(thread);
   thread.Start();
}

foreach (var thread in threads) {
    thread.Join();
}
青芜 2024-10-28 20:37:45

如果您使用的是 3.5,那么您可以编写自己的 CountdownEvent,如果您使用的是 4.0,那么您可以使用内置的 CountdownEvent 执行如下操作:

CountdownEvent = new CountdownEvent(taskGroups.Count());

for (int i = 0; i < taskGroups.Count(); i++) 
{
    int item = i; // copy i locally
    ThreadStart t = delegate 
    { 
        RunThread(taskGroups[item]); 
        latch.Signal();
    };
    new Thread(t).Start();
}

latch.Wait();

latch.Wait() 将导致您的代码阻塞,直到线程全部完成。此外,您可能想稍微改变一下启动线程的方式:

CountdownEvent = new CountdownEvent(taskGroups.Count());

for (int i = 0; i < taskGroups.Count(); i++) 
{
    int item = i; // copy i locally
    Thread t = new Thread(()=>
    { 
        RunThread(taskGroups[item]); 
        latch.Signal();
    });
    t.IsBackground = true;
    t.Start();
}

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:

CountdownEvent = new CountdownEvent(taskGroups.Count());

for (int i = 0; i < taskGroups.Count(); i++) 
{
    int item = i; // copy i locally
    ThreadStart t = delegate 
    { 
        RunThread(taskGroups[item]); 
        latch.Signal();
    };
    new Thread(t).Start();
}

latch.Wait();

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:

CountdownEvent = new CountdownEvent(taskGroups.Count());

for (int i = 0; i < taskGroups.Count(); i++) 
{
    int item = i; // copy i locally
    Thread t = new Thread(()=>
    { 
        RunThread(taskGroups[item]); 
        latch.Signal();
    });
    t.IsBackground = true;
    t.Start();
}

latch.Wait();

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).

书间行客 2024-10-28 20:37:45

您可以使用 Thread.Join 确保每个单独的线程都已完成跑步。

You can use Thread.Join to make sure that each individual thread has finished running.

許願樹丅啲祈禱 2024-10-28 20:37:45

您可以将公共静态整数字段添加到主线程,在每个子线程中,完成后将其加一,然后在主线程中等待(循环),直到该变量等于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().

樱花细雨 2024-10-28 20:37:45

首先考虑使用 任务

无论如何,如果您想等待所有线程,您可以调用 Thread.Join:

var threads = new List<Thread>();    
for (int i = 0; i < taskGroups.Count(); i++) {
    ThreadStart t = delegate { RunThread(taskGroups[i]); };
    var thread = new Thread(t);
    threads.Add(thread);
    thread.Start();
}

threads.ForEach(a => a.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:

var threads = new List<Thread>();    
for (int i = 0; i < taskGroups.Count(); i++) {
    ThreadStart t = delegate { RunThread(taskGroups[i]); };
    var thread = new Thread(t);
    threads.Add(thread);
    thread.Start();
}

threads.ForEach(a => a.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.

就是爱搞怪 2024-10-28 20:37:45

您可以检查每个 Thread 对象的 ThreadState 属性。

You can check the ThreadState property of each Thread object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文