Thread.Join

How do you ensure that all the threads have been completed? I mean you want to wait in the main thread until all the threads have bee completed. Lets take an simple example.


namespace My_Samples
{
    using System;
    using System.Threading;

    public class MyClass
    {
        public void MyMethod()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("My Method - " + i.ToString());
                Thread.Sleep(50);
            }
            Console.WriteLine("MyMethod completed...");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyClass obj = new MyClass();
            ThreadStart method = new ThreadStart(obj.MyMethod);
            Thread thread = new Thread(method);
            thread.Start();
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Main Method - Iteration 1 - " + i.ToString());
                Thread.Sleep(50);
            }
            Console.WriteLine("Main method iteration is completed...");

            //Here I want to wait until the thread completes
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Main Method - Iteration 2 - " + i.ToString());
                Thread.Sleep(50);
            }
            Console.ReadLine();
        }
    }
}

When you run the code, the output is something looks below.
Main Method - Iteration 1 - 0
My Method - 0
Main Method - Iteration 1 - 1
My Method - 1
Main Method - Iteration 1 - 2
My Method - 2
Main Method - Iteration 1 - 3
My Method - 3
Main Method - Iteration 1 - 4
My Method - 4
Main method iteration is completed
Main Method - Iteration 2 - 0
My Method - 5
Main Method - Iteration 2 - 1
My Method - 6
Main Method - Iteration 2 - 2
My Method - 7
Main Method - Iteration 2 - 3
My Method - 8
Main Method - Iteration 2 - 4
My Method - 9
MyMethod completed...

Actually I want to wait for the thread to complete after first iteration in the main method. There the Thread.Join helps. Let's introduce this code after the first iteration in the main thread and see what would be the output.


namespace My_Samples
{
    using System;
    using System.Threading;

    public class MyClass
    {
        public void MyMethod()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("My Method - " + i.ToString());
                Thread.Sleep(50);
            }
            Console.WriteLine("MyMethod completed...");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyClass obj = new MyClass();
            ThreadStart method = new ThreadStart(obj.MyMethod);
            Thread thread = new Thread(method);
            thread.Start();
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Main Method - Iteration 1 - " + i.ToString());
                Thread.Sleep(50);
            }
            Console.WriteLine("Main method iteration is completed...");
            //Here I want to wait until the thread completes
            thread.Join();
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Main Method - Iteration 2 - " + i.ToString());
                Thread.Sleep(50);
            }
            Console.ReadLine();
        }
    }
}

Now if you look at the output, that will something looks like below. Here the Main thread (or the current thread) wait until the specified thread completes.

Main Method - Iteration 1 - 0
My Method - 0
Main Method - Iteration 1 - 1
My Method - 1
Main Method - Iteration 1 - 2
My Method - 2
Main Method - Iteration 1 - 3
My Method - 3
Main Method - Iteration 1 - 4
My Method - 4
Main method iteration - 1 is completed...
My Method - 5
My Method - 6
My Method - 7
My Method - 8
My Method - 9
MyMethod completed...
Main Method - Iteration 2 - 0
Main Method - Iteration 2 - 1
Main Method - Iteration 2 - 2
Main Method - Iteration 2 - 3
Main Method - Iteration 2 - 4

No comments:

Post a Comment