views:

175

answers:

7

What is the significance of Thread.Join method in c sharp .net ? MSDN says that it blocks the calling thread until a thread terminates. Can anybody explain this with a simple example ?

+1  A: 

Suppose you have a main thread that delegates some work to worker threads. The main thread needs some results that the workers are computing, so it cannot continue until all worker threads have finished.

In this scenario, the main thread would call Join() on each of the worker threads. After all the Join() calls have returned, the main thread knows that all worker threads have finished, and that the computed results are available for its consumption.

Thomas
A: 

Imagine your program runs in Thread1. Then you need to start some calculation or processing - you start another thread - Thread2. Then if you want your Thread1 wait until Thread2 ends you execute Thread1.Join(); and Thread1 will not continue its execution until Thread2 finishes.

Here is description in MSDN.

Andrew Bezzub
MSDN says there that "Blocks the calling thread until a thread terminates, while continuing to perform standard COM and SendMessage pumping.". What is the meaning of performing standard COM ? What is the meaning of sendMessage pumping ?
Techee
A: 

The simple example approach:

public static void Main(string[] args)
{
    Console.WriteLine("Main thread started.");

    var t = new Thread(() => Thread.Sleep(2000));

    t.Start();

    t.Join();

    Console.WriteLine("Thread t finished.");
}

The program starts by printing a message to the screen and then starting a new thread that just pauses for 2 seconds before terminating. The last message is only printed after the t thread finishes executing because the Join method call blocks the current thread until the t thread terminates.

João Angelo
It's simple, but also quite useless: A Thread.Start immediately followed by a Thread.Join defeats the whole purpose of using a second thread. (I know it's a toy example, but still...)
Heinzi
@Heinzi, it merely demonstrates the underlying behavior of the `Join` method as requested. It was never intended to depict a real-world scenario.
João Angelo
A: 
static void Main()
{
 Thread t = new Thread(new ThreadStart(some delegate here));
 t.Start();
 Console.WriteLine("foo");
 t.Join()
 Console.WriteLine("foo2");
}

In your delegate you would have another call like this:

Console.WriteLine("foo3");

Output is:

foo
foo3
foo2
citronas
+3  A: 

Join() is basically while(thread.running){}

{
  thread.start()
  stuff you want to do while the other thread is busy doing its own thing concurrently
  thread.join()
  you won't get here until thread has terminated.
} 
Spike
+2  A: 
int fibsum = 1;

Thread t = new Thread(o =>
                          {
                              for (int i = 1; i < 20; i++)
                              {
                                  fibsum += fibsum;
                              }
                          });

t.Start();
t.Join(); // if you comment this line, the WriteLine will execute 
          // before the thread finishes and the result will be wrong
Console.WriteLine(fibsum);
legenden
A: 

This is just to add to the existing answers, that explain what Join does.

Calling Join also has the side effect of allowing the message pump to handle messages. See this knowledge base article for situations where this may be relevant.

Brian Rasmussen