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 couldnt 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
{ Output of this code will be: In sample Continues execution of main thread
|