title: Windows Store App how to continuously read from a TCP socket and update the UI when "\n" is received
body: <p>I am working with <a href="http://donatas.xyz/streamsocket-tcpip-client.html" rel="nofollow">this</a> code in order to comunicate with another program through TCP which acts as a server. My app is also a Windows Store App and I just added the 3 methods to my code. The server is not made by me, I can't modify it in any way. Connection and sending messages works fine. After I give a specific command to the server, it sends back a continuous stream composed of strings that end in "\r\n" in order to see when a message ends, something like this: <code>"string1\r\nstring2\r\n"</code> and so on, as long as there is a connection with it. Note that sending the command works because I get a visual response from the server.</p>
  
  <p>I can not find a way to display the individual strings in my app's UI, I think my problem lies in the <code>read()</code> method, because the stream never "consumes":</p>
  
  <pre><code>public async Task&lt;String&gt; read() 
      {
          DataReader reader;
          StringBuilder strBuilder;
  
          using (reader = new DataReader(socket.InputStream))
          {
              strBuilder = new StringBuilder();
  
              // Set the DataReader to only wait for available data (so that we don't have to know the data size)
              reader.InputStreamOptions = Windows.Storage.Streams.InputStreamOptions.Partial;
              // The encoding and byte order need to match the settings of the writer we previously used.
              reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
              reader.ByteOrder = Windows.Storage.Streams.ByteOrder.LittleEndian;
  
              // Send the contents of the writer to the backing stream. 
              // Get the size of the buffer that has not been read.
              await reader.LoadAsync(256);
  
              // Keep reading until we consume the complete stream.
              while (reader.UnconsumedBufferLength &gt; 0)
              {
                  strBuilder.Append(reader.ReadString(reader.UnconsumedBufferLength));
                  await reader.LoadAsync(256);
              }
  
              reader.DetachStream();
              return strBuilder.ToString();
          }
      }
  </code></pre>
  
  <p>I have an event on a button that calls <code>send()</code> having a parameter the string command I wish to send. At first, I simply tried <code>textBox.Text = await read();</code> after calling the <code>send()</code> method, nothing appeared in the <code>textBox</code>. Next, I tried making the <code>read()</code> method to not return anything and putting <code>textBox.Text = strBuilder.ToString();</code> in different places inside <code>read()</code>. Finally, I discovered if I put it inside <code>while (reader.UnconsumedBufferLength &gt; 0)</code> after <code>strBuilder.Append(reader.ReadString(reader.UnconsumedBufferLength));</code> the <code>textBox</code> gets updated, although I'm not sure if the strings really appear correctly, but my UI becomes unresponsive, probably because it gets stuck in the <code>while</code> loop. I searched the internet for multiple examples, including how to do it in a separate thread, unfortunately my experience is entry-level and this is the best I could do, I don't know how to adapt the code any further. I hope I have been explicit enough. Also, I don't mind if you show me a different, better way of updating the UI</p>