title: How to use Task.WhenAny with ReadLineAsync to get data from any TcpClient
body: <p>working my way through all that is async / await (coming from threadpools) and I've hit an interesting challenge.</p>
  
  <p>I have a TCP Server running in a WPF application that accepts clients and stores them in a <code>List&lt;&gt;</code> like such:</p>
  
  <pre><code>private List&lt;Client&gt; clients = new List&lt;Client&gt;();
  while (running &amp;&amp; clientCount &lt;= maxClients)
  {
      Client client = new Client(await server.AcceptTcpClientAsync());
      await client.WriteLineAsync("Connected to the Server!");
      clients.Add(client);
      clientCount++;
  }
  </code></pre>
  
  <p>So what I want to do is iterate through the list of my Clients and if any data is received, I want to append it to a textbox. I realize <strong>this may not be the best way</strong> to achieve this, and I'm open to suggestions, but this is how I currently have it structured.</p>
  
  <p>A button starts the loop and continuously calls and awaits <code>AllReadLineAsync()</code></p>
  
  <pre><code>private async void btnStartReadLoopClick(object sender, RoutedEventArgs e)
  {
      btnStartReadLoop.IsEnabled = false;
      while(server.clientCount &gt; 0)
      {
          string text = await server.AllReadLineAsync();
          txtOutputLog.AppendText("[client] " + text + "\n");
      }
  }
  </code></pre>
  
  <p>which is this function:</p>
  
  <pre><code>public async Task&lt;string&gt; AllReadLineAsync()
  {
      var tasklist = new List&lt;Task&lt;string&gt;&gt;();
  
      foreach (var client in clients)
          tasklist.Add(client.ReadLineAsync());
  
      while (tasklist.Count &gt; 0)
      {
          Task&lt;string&gt; finishedTask = await Task.WhenAny(tasklist);
          if (finishedTask.Status == TaskStatus.RanToCompletion)
              return await finishedTask;
  
          tasklist.Remove(finishedTask);
      }
  
      return "Error: No task finished";
  }
  </code></pre>
  
  <p>This function iterates through the list of clients and creates a <code>List&lt;Tast&lt;string&gt;&gt;</code> of all the <code>ReadLineAsync()</code> tasks.</p>
  
  <p>At any given time, I may only have 1 or 2 clients actually sending data, so I can't <code>WhenAll()</code> and I've tried <code>WhenAny()</code> and <code>WaitAny()</code> without success. </p>
  
  <p><strong>Note to future googlers:</strong> <code>WaitAny()</code> is like <code>Wait()</code> and is blocking. Do not do this on a UI thread. Instead use <code>WhenAny()</code> and await it.</p>
  
  <p>So my current implementation kind of works, but I can't figure out this bug where messages will build up from 1 client if the other clients don't send data.</p>
  
  <p><strong>TL;DR:</strong> Am I using <code>WhenAny()</code> correctly or is there a better way for me to await ReadLineAsync and pass the result to a textbox?</p>
  
  <p><strong>EDIT:</strong> Here's the behavior I'm seeing
  I typed in this order: Left, Right, Left 2, Right 2, Left 3, Right 3 
  and it appears as if some messages are being dropped?</p>
  
  <p><a href="https://i.stack.imgur.com/hcqxe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hcqxe.png" alt="enter image description here"></a></p>
  
  <p><strong>EDIT 2:</strong> I found the source of the code snippet I copied on the MSDN blog: <a href="https://blogs.msdn.microsoft.com/pfxteam/2012/08/02/processing-tasks-as-they-complete/" rel="nofollow noreferrer">https://blogs.msdn.microsoft.com/pfxteam/2012/08/02/processing-tasks-as-they-complete/</a></p>
  
  <p>This code snippet is meant specifically to iterate through a list of tasks ensuring they all are completed. I don't care if tasks are duplicated though so I need to modify the code to always check the entire tasklist instead of removing any tasks.</p>