Thomas
Thomas

Reputation: 34198

A basic query about Thread & join usage c#

some time i used thread in my program but i never use join(). i got something about join() like below

Join will stop the current running thread and let the "Join" thread runs until it finishes.

static void Main()
{
  Thread t = new Thread (Go);
  t.Start();
  t.Join();
  Console.WriteLine ("Thread t has ended!");
}

static void Go()
{
  for (int i = 0; i < 10; i++) Console.Write ("y");
}

from the above code i just do not understand what kind of important role the join() is playing here. please discuss about the join usage.

if possible give me a small realistic code for join() as a result i can understand the good use of join().

also guide me join() can be use in multi threaded environment. thanks

Upvotes: 1

Views: 113

Answers (5)

Kamil Krasinski
Kamil Krasinski

Reputation: 529

If you remove t.Join() from your code application it will end execution before you can be certain that Go() method executed.

Join is very useful if you have got 2 or more methods that can be executed at the same time but all of them need to finalize, before you can execute a method that depends on them.

Please look at below code:

static void Main(string[] args)
        {
        Thread t1 = new Thread(Method1);
        Thread t2 = new Thread(Method2);
        t1.Start();
        t2.Start();
        Console.WriteLine("Both methods are executed independently now");
        t1.Join(); // wait for thread 1 to complete
        t2.Join(); // wait for thread 2 to complete
        Console.WriteLine("both methods have completed");
        Method3(); // using results from thread 1 and thread 2 we can execute  method3 that can use results from Method1 and Method2

        }

Upvotes: 3

Steven Mills
Steven Mills

Reputation: 2381

Using the code you posted as an example, if it were written like this:

static void Main()
{
  Thread t = new Thread (Go);
  t.Start();
  Console.WriteLine ("Thread t has ended!");
}

static void Go()
{
  for (int i = 0; i < 10; i++) Console.Write ("y");
}

your output would be something along the lines of:

yyy Thread t has ended!yyyyyyy

meaning Go() runs simultaneously with Console.WriteLine ("Thread t has ended!");

By adding t.join(), you wait till your thread is finished before continuing. This is useful if you only want a section of your code to run simultaneously with a thread.

Upvotes: 4

Dmitrii Dovgopolyi
Dmitrii Dovgopolyi

Reputation: 6301

Consider some game example.

static void Main()
{
  Thread t = new Thread (LoadMenu);
  t.Start();
  Showadvertisement();
  t.Join();
  ShowMenu();
}

static void LoadMenu()
{
    //loads menu from disk, unzip textures, online update check.
}

static void Showadvertisement()
{
    //Show the big nvidia/your company logo fro 5 seconds
}

static void ShowMenu()
{
   //everithing is already loaded. 
}

The point is you can make multiple things in two threads, but at one point you should synchronize them and be sure, that everything allready completed

Upvotes: 2

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

Blocks the calling thread until the termination of a thread.

Note : You can also blocks the calling thread until a thread terminates or the specified time elapses, while continuing to run the standard COM and SendMessage pumping. Supported by the. NET Compact Framework.

link : http://msdn.microsoft.com/fr-fr/library/system.threading.thread.join(v=vs.80).aspx

Upvotes: 2

NthDeveloper
NthDeveloper

Reputation: 1009

.Join() call waits until the thread ends. I mean this call will return when your Go method returns.

Upvotes: 0

Related Questions