You can use the Join method to wait until a thread finishes; this method will return when the thread is finished executing. Here are the various forms of this method:
Sub Join- Waits for a thread to die.
Function Join(TimeOut As Integer) As Boolean -Waits for the thread to die or for a specific timeout, given as a number of milliseconds, to elapse. Returns True if the thread died, False if the call timed out.
Function Join(TimeOut As TimeSpan) As Boolean- Waits for the thread to die or for a specific timeout, given as a TimeSpan object, to elapse. Returns True if the thread died, False if the call timed out.
We used the Join method in the SynchronizeThreads example, discussed in the In Depth section, which was to wait until the two threads in that example were done executing before displaying the value those threads were incrementing. Here's how that looked in code:
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Thread1.Start() Thread2.Start() Thread1.Join() Thread2.Join() TextBox1.Text = c.Total End Sub
And that's all there is to it-for more information on this example, see the In Depth section of this chapter.