title: What's the difference between Thread.Sleep() and Thread.SpinWait()
body: <p>I have a WinForm application that uses a BackGroundWorker to create a TCP Client and send some data to a remote server.
  When the socket finish close the connection and the BGW exits from the DoWork Sub.</p>
  
  <p>In the RunWorkerCompleted Sub I must to wait a packet from the remote server, so I have always running a TCP server that fills a Friend String type variable and indicates that the whole packet is received using a Boolean type variable(flag).</p>
  
  <p>So I must to wait that flag to becomes True to process the data that must be in the String type variable; but I don't want to hang up the GUI so I see that exist a method called SpinWait.</p>
  
  <p>So, Can this code works?</p>
  
  <p>There's any way to get out if the loop if the flag doesn't to true 5 minutes later?</p>
  
  <pre><code>Private Sub BGW1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BGW1.RunWorkerCompleted
          While Not AckReady
              System.Threading.Thread.SpinWait(500)
          End While
  
          'Here process the received data from TCP server
  
          TmrReport.Start()
  
  End Sub 
  </code></pre>
  
  <p>Another things is, How many iterations represent 500mS?</p>