Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for threads to complete

Tags:

c#

    private void button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 15; i++)
        {
            Thread nova = new Thread(Method);
            nova.Start();
        }
        listBox1.Items.Add("Some text");
    }

    private void Method()
    {
        for (int i = 0; i < 15; i++)
        {
            Console.WriteLine(i);
        }

    }

This code does write: Some text and then numbers 111222333..... I would like that it writes 111122223333.... and then on the end Some text. is it possible to do that with threads (parent thread wait for child threads)? or do i have to use something else?

like image 222
Car90 Avatar asked Jun 22 '12 16:06

Car90


People also ask

Which method is used for waiting to completion of the thread?

The wait() and join() methods are used to pause the current thread. The wait() is used in with notify() and notifyAll() methods, but join() is used in Java to wait until one thread finishes its execution.

How do you wait for a thread?

wait() causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0). The current thread must own this object's monitor.

How do you wait for a runnable to finish?

Create an Object called lock . Then after runOnUiThread(myRunnable); , you can call lock. wait() . And when your myRunnable is finish it's job, call lock.

How do you wait until all threads are finished C++?

Waiting for threads to finish To wait for a thread use the std::thread::join() function. This function makes the current thread wait until the thread identified by *this has finished executing.


2 Answers

You need to keep track of all the threads, and use Thread.Join on each one. This waits until the specified thread terminates, and then continues executing. Like this:

var threads = new List<Thread>();
for (int i = 0; i < 15; i++)
{
    Thread nova = new Thread(Method);
    nova.Start();
    threads.Add(nova);
}
foreach (var thread in threads)
    thread.Join();
listBox1.Items.Add("Some text");
like image 178
Roman Starkov Avatar answered Oct 06 '22 00:10

Roman Starkov


I suggest to use TPL to get this done. You won't need to spawn so many threads. By default TPL will use thread pool for that:

using System;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        private const int TaskCount = 15;

        static void Main(string[] args)
        {
            var tasks = new Task[TaskCount];
            for (var index = 0; index < TaskCount; index++)
            {
                tasks[index] = Task.Factory.StartNew(Method);
            }
            Task.WaitAll(tasks);

            Console.WriteLine("Some text");
        }

        private static void Method()
        {
            for (int i = 0; i < 15; i++)
            {
                Console.WriteLine(i);
            }
        }
    }
}
like image 34
Sergei B. Avatar answered Oct 06 '22 01:10

Sergei B.