What is the purpose of Thread.Join() method in .NET?

Thread.Join() method belongs to System.Threading namespace. If you want a thread to wait until another thread finishes its execution, then you can do it using Thread.Join(). Assume that you have two threads namely sample1Thread and sample2Thread. You have started sample1Thread and then started sample2Thread.




Now if you want to block execution of sample1Thread until sample2Thread finishes its execution, then you can do it by calling sample2Thread.join(). This code statement will put sample1Thread on wait until sample2Thread execution gets completed. After sample2Thread finishes, sample1Thread will continue its execution.

What if sample2Thread runs for a long time and sample1Thread cannot wait until it gets completed, then instead of calling sample2Thread.join(), you can call sample2Thread.join(10000) where 10000 means that sample1Thread will wait for next 10 seconds within which it expects the sample2Thread to finish its execution. If sample2Thread couldn’t finish its job in 10 seconds, then the sample1Thread will continue its execution even if sample2Thread is still executing.

Here is an example that makes main Thread to wait until sampleThread finishes its execution:

class sampleClass {
public static void Main() {
Thread sampleThread = new Thread(new ThreadStart(sample));
sampleThread.Start();
sampleThread.Join();
Console.WriteLine("Continues execution of main thread");
}
static void sample() {
Console.WriteLine("In sample");
for (int index = 0; index < 2; index++) {
Console.WriteLine("Iteration {0}", index);
Thread.Sleep(2000);
}
}
}

Output of this code will be:

In sample
Iteration 0
Iteration 1

Continues execution of main thread

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| What is Private Access Modifier in C#? |Introducing Microsoft SQL Server 2000 Desktop Engine | What is Protected Access Modifier in C#? | What is Protected Internal Access Modifier in C#? | What is Public Access Modifier in C#? | What is the difference between virtual and abstract keywords in .NET? | What is the importance of Microsoft Application Blocks in .NET Architecture? | What is the need for Factory Method in C# | What is the purpose of ArrayList in .NET? | What is the purpose of Datareader in ADO.NET? | What is the purpose of Dataset in ADO.NET? | What is the purpose of finally block in C#? | What is the purpose of interlocked class in .NET? | What is the purpose of main() function in C# | What is the purpose of ManualResetEvent in .NET? | What is the purpose of sealed method in C#? | What is the purpose of Thread.Join() method in .NET? | What is the purpose of Thread.Sleep() method in .NET? | What is the purpose of throw keyword in C#? | What is the usage of ENUM in .NET? |


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.