title: Finish two tasks then printing something
body: <p>I have three tasks, one is producer, then consumer and the last one is to print something after finishing the first two. However the code doesn't reach the last task, which means no printing.</p>
  
  <pre><code> while (true)
              {
                  ThreadEvent.WaitOne(waitingTime, false);
  
                  lock (SyncVar)
                  {
                      collection = new BlockingCollection&lt;string&gt;(4);
                      Task producer = Task.Run(() =&gt;
                       {
                           if (list.Count &gt; 0)
                               Console.WriteLine("Block begin");
                           while (!collection.IsAddingCompleted)
                           {
                               var firstItem = list.FirstOrDefault();
                               collection.TryAdd(firstItem);
                               list.Remove(firstItem);
                           }
                           collection.CompleteAdding();
                       });
                      Task consumer = Task.Run(() =&gt; DoConsume());
                      Task endTask = consumer.ContinueWith(i =&gt; Console.WriteLine("Block end"));// not print this line, why?
                      Task.WaitAll(producer, consumer, endTask);
                      if (ThreadState != State.Running) break;
                  }
              }
  </code></pre>
  
  <p>Please look at my code logic.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>For `DoConsume', it is complicated.</p>
  
  <pre><code>public void DoConsume()
      {
          if (collection.Count &gt; 0)
              Console.WriteLine("There are {0} channels to be processed.", collection.Count);
  
          var workItemBlock = new ActionBlock&lt;string&gt;(
          workItem =&gt;
          {
              bool result =ProcessEachChannel(workItem);
          });
  
          foreach (var workItem in collection.GetConsumingEnumerable())
          {
               workItemBlock.Post(workItem);
          }
  
          workItemBlock.Complete();
      }
  </code></pre>