What is the purpose of Thread.Join() method in .NET?
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:
Output of this code will be:
Continues execution of main thread
to our mailing list and receive new articles
: We never rent, trade, or sell my email lists to
Visit .NET Programming Tutorial Homepage
| 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? |