title: How to complete a Channel right way? How to use multiple channels?
body: <p>I have a list of data and want to create the number of Tasks corresponding to the number of elements in the list. But I don't know how to Complete a Channel properly.</p>
  <p>My code, but the Channel doesn't close as I expect.</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Channels;
  using System.Threading.Tasks;
  
  namespace Ding.LearningNewThings
  {
      public class MultipleChannel
      {
          public static async Task RunMiltipleChannel()
          {
              List&lt;Place&gt; listPlace = Place.InitData();
  
              Channel&lt;Position&gt; _dataChannel = Channel.CreateUnbounded&lt;Position&gt;();
  
              var listTask = new Task[11];
  
              var listStatus = new bool[10];
  
              for (int k = 0; k &lt; listPlace.Count(); k++)
              {
                  var place = listPlace[k];
                  listTask[k] = Task.Run(async () =&gt;
                  {
                      int kk = k;
                      int count = 0;
  
                      Random r = new Random();
  
                      while (count &lt; 10)
                      {
                          int id = r.Next(1, 1000);
                          var position = new Position()
                          {
                              ID = id,
                              Name = $&quot;Postion{id}&quot;,
                              PlaceID = place.ID,
                              PlaceName = place.Name
                          };
  
                          Console.WriteLine($&quot;WRITE: Position ID: {position.ID}, Postion Name: {position.Name}&quot;);
                          await _dataChannel.Writer.WriteAsync(position);
                          count++;
                      }
  
                      lock (listStatus)
                      {
                          if(count == 10)
                          {
                              listStatus[k] = true;
                          }
  
                          bool isStop = true;
                          
                          foreach(var status in listStatus)
                          {
                              if (!status)
                              {
                                  isStop = false;
                              }
                          }
  
                          if (isStop)
                          {
                              _dataChannel.Writer.Complete();
                              Console.WriteLine(&quot;Stopped&quot;);
                          }
                      }
  
                  });
              }
  
  
              listTask[10] = Task.Run(async () =&gt;
              {
                  while (await _dataChannel.Reader.WaitToReadAsync())
                  {
                      await Task.Delay(100);
  
                      var data = await _dataChannel.Reader.ReadAsync();
  
                      Console.WriteLine($&quot;READ: Position ID: {data.ID}, Postion Name: {data.Name}&quot;);
                  }
              });
  
              await Task.WhenAll(listTask);
  
          }
      }
  
      public class Place
      {
          public int ID { get; set; }
          public string Name { get; set; }
  
          public static List&lt;Place&gt; InitData()
          {
              var listData = new List&lt;Place&gt;();
  
              for (int i = 0; i &lt; 10; i++)
              {
                  var data = new Place()
                  {
                      ID = i,
                      Name = $&quot;Postion{i}&quot;,
  
                  };
  
                  listData.Add(data);
              }
              return listData;
          }
      }
  
      public class Position
      {
          public int ID { get; set; }
          public int PlaceID { get; set; }
          public string PlaceName { get; set; }
          public string Name { get; set; }
  
          public static List&lt;Position&gt; InitData()
          {
              var listData = new List&lt;Position&gt;();
  
              for (int i = 0; i &lt; 10; i++)
              {
                  var data = new Position()
                  {
                      ID = i,
                      Name = $&quot;Postion{i}&quot;
                  };
  
                  listData.Add(data);
              }
              return listData;
          }
      }
  
  
  }
  
  </code></pre>
  <p>In case I want to create separate Channels for each Task, how do I Complete them? Please give me an example.</p>