title: Make UI thread wait until another thread is finished
body: <p>I have some method run in the UI thread. Inside this method a new thread is created. I need UI thread waits until this new thread is finished because I want to wait for some results to be able to continue the method. But I don´t want to have UI frozen so meanwhile UI thread is waiting, it is also responding with UI.</p>
  
  <p>Here is what I have now:</p>
  
  <pre><code>Dim _WaitHandler As New System.Threading.AutoResetEvent(False)
  
  Public Sub Method()
  
     For i As Integer = 0 To 100
  
        Dim myThread As Thread = New Thread(AddressOf ThreadMethod)
        myThread.Start()
  
        _WaitHandler.WaitOne()
  
        //next loop or exit?
     Next
  
     //this code I want to process after myThread is finished
     //but also I don´t want to freeze UI during WaitOne method
  
  End Sub
  
  Private Sub ThreadMethod()
  
     //do something...
     _WaitHandler.Set()
  
  End Sub
  </code></pre>
  
  <p>It seems that during <code>WaitOne</code> method UI thread sleeps and it doesn´t response so UI is frozen. Have you any idea where is the problem? Thank you guys. </p>
  
  <p><strong>EDIT</strong> Here is a thing a forgot to write you: myThread creating is inside a for cycle. After myThread is finished, based on a results from myThread I have to decide If I should continue in the cycle (next loop) or exit cycle. My code is edited.</p>
  
  <p>If I use a BackgroundWorker, UI thread will automatically continue with next loop.</p>