title: How to use UdpClient.BeginReceive in a loop
body: <p>I want to do this</p>
  
  <pre><code>for (int i = 0; i &lt; 100; i++ )
  {
      Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
  }
  </code></pre>
  
  <p>But instead of using <code>UdpClient.Receive</code>, I have to use <code>UdpClient.BeginReceive</code>. The problem is, how do I do that? There aren't a lot of samples using <code>BeginReceive</code>, and the MSDN example is not helping at all. Should I use <code>BeginReceive</code>, or just create it under a separate thread?</p>
  
  <p>I consistently get <code>ObjectDisposedException</code> exception. I only get the first data sent. The next data will throw exception.</p>
  
  <pre><code>public class UdpReceiver
  {
      private UdpClient _client;
      public System.Net.Sockets.UdpClient Client
      {
          get { return _client; }
          set { _client = value; }
      }
      private IPEndPoint _endPoint;
      public System.Net.IPEndPoint EndPoint
      {
          get { return _endPoint; }
          set { _endPoint = value; }
      }
      private int _packetCount;
      public int PacketCount
      {
          get { return _packetCount; }
          set { _packetCount = value; }
      }
      private string _buffers;
      public string Buffers
      {
          get { return _buffers; }
          set { _buffers = value; }
      }
      private Int32 _counter;
      public System.Int32 Counter
      {
          get { return _counter; }
          set { _counter = value; }
      }
      private Int32 _maxTransmission;
      public System.Int32 MaxTransmission
      {
          get { return _maxTransmission; }
          set { _maxTransmission = value; }
      }
  
      public UdpReceiver(UdpClient udpClient, IPEndPoint ipEndPoint, string buffers, Int32 counter, Int32 maxTransmission)
      {
          _client = udpClient;
          _endPoint = ipEndPoint;
          _buffers = buffers;
          _counter = counter;
          _maxTransmission = maxTransmission;
      }
      public void StartReceive()
      {
          _packetCount = 0;
          _client.BeginReceive(new AsyncCallback(Callback), null);
      }
  
      private void Callback(IAsyncResult result)
      {
          try
          {
              byte[] buffer = _client.EndReceive(result, ref _endPoint);
              // Process buffer
              MainWindow.Log(Encoding.ASCII.GetString(buffer));
              _packetCount += 1;
              if (_packetCount &lt; _maxTransmission)
              {
                  _client.BeginReceive(new AsyncCallback(Callback), null);
              }
          }
          catch (ObjectDisposedException ex) 
          {
              MainWindow.Log(ex.ToString());
          }
          catch (SocketException ex) 
          { 
              MainWindow.Log(ex.ToString()); 
          }
          catch (System.Exception ex)
          {
              MainWindow.Log(ex.ToString()); 
          }
      }
  }
  </code></pre>
  
  <p>What gives?</p>
  
  <p>By the way, the general idea is:</p>
  
  <ol>
  <li>Create tcpclient manager.</li>
  <li>Start sending/receiving data using udpclient.</li>
  <li>When all data has been sent, tcpclient manager will signal receiver that all data has been sent, and udpclient connection should be closed.</li>
  </ol>