title: Ending a thread safely when using UDP Receive
body: <p>I'm creating a thread that runs a UDP Client that receives a message, after it receives the message I want to close the UDP client and then end the thread, but I dont know how to end the thread since "Receive" always runs until it gets an answer.</p>
  
  <p>This is my code so far:</p>
  
  <pre><code>private void RecieveChallenge()
  {
      UdpClient client = new UdpClient(26000);
      IPEndPoint remoteIp = new IPEndPoint(IPAddress.Any, 0);
  
      Byte[] receivedBytes = client.Receive(ref remoteIp);
      string ipAddress = Encoding.ASCII.GetString(receivedBytes);
  }
  </code></pre>
  
  <p>The important line is client.Receive(ref remoteIp);</p>
  
  <p>Here is how I start my thread:</p>
  
  <pre><code>Thread recieveChallengeThread = new Thread(new ThreadStart(RecieveChallenge));
  recieveDataThread.Start();
  </code></pre>