title: High CPU % when using delegate
body: <p>I'm running the following piece of code which uses a delegate to return an asynchronous Network Stream:</p>
  
  <pre><code>static void Main(string[] args)
  {
      NetworkStream myNetworkStream;
      Socket socket;
      IPEndPoint maxPort = new IPEndPoint(IPAddress.Parse("xxx.xxx.xxx.xxx"), xxxx);
  
      socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
      socket.Connect(maxPort);
  
      myNetworkStream = new NetworkStream(socket);
  
      byte[] buffer = new byte[1024];
      int offset = 0;
      int count = 1024;
  
      string Command = "LOGIN,,,xxxx\n";
      ASCIIEncoding encoder = new ASCIIEncoding();
  
      myNetworkStream.BeginRead(buffer, offset, count, new AsyncCallback(OnBeginRead), myNetworkStream);
      myNetworkStream.Write(encoder.GetBytes(Command), 0, encoder.GetByteCount(Command));
  
      while (true) { }
  }
  
  public static void OnBeginRead(IAsyncResult ar)
  {
      NetworkStream ns = (NetworkStream)ar.AsyncState;
      int bufferSize = 1024;
      byte[] received = new byte[bufferSize];
  
      ns.EndRead(ar);
  
      int read;
  
      while (true)
      {
          if (ns.DataAvailable)
          {
              string result = String.Empty;
  
              read = ns.Read(received, 0, bufferSize);
              result += Encoding.ASCII.GetString(received);
              received = new byte[bufferSize];
  
              result = result.Replace(" ", "");
              result = result.Replace("\0", "");
              result = result.Replace("\r\n", ",");
  
              Console.WriteLine(result);
          }
      }
  }
  </code></pre>
  
  <p>It works, but my CPU usage is through the roof (50% on an Intel Core i3), so obviously I'm doing it wrong, but how so?</p>
  
  <p>Thanks</p>