title: Feeding a cancellationtoken to a Task does nothing?
body: <p>I have two examples, straight from microsoft, where these examples seem to have nothing to do with cancellation token, because I can remove the token that is fed to the task, and the result is the same. So my question is: What is the cancellation token for, and why the poor examples? Am I missing something..? :)</p>
  
  <pre><code>using System;
  using System.Threading;
  using System.Threading.Tasks;
  namespace Chapter1.Threads
  {
      public class Program
      {
          static void Main()
          {
              CancellationTokenSource cancellationTokenSource =
                  new CancellationTokenSource();
              CancellationToken token = cancellationTokenSource.Token;
              Task task = Task.Run(() =&gt;
              {
                  while (!token.IsCancellationRequested)
                  {
                      Console.Write(“*”);
                      Thread.Sleep(1000);
                  }
                  token.ThrowIfCancellationRequested();
              }, token);
              try
              {
                  Console.WriteLine(“Press enter to stop the task”);
                  Console.ReadLine();
                  cancellationTokenSource.Cancel();
                  task.Wait();
              }  
              catch (AggregateException e)
              {
                  Console.WriteLine(e.InnerExceptions[0].Message);
              }
              Console.WriteLine(“Press enter to end the application”);
              Console.ReadLine();
          }
      }
  }
  </code></pre>
  
  <p>Code example2:
  <a href="https://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken(v=vs.110).aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken(v=vs.110).aspx</a></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Threading;
  using System.Threading.Tasks;
  
  public class Example
  {
     public static void Main()
     {
        // Define the cancellation token.
        CancellationTokenSource source = new CancellationTokenSource();
        CancellationToken token = source.Token;
  
        Random rnd = new Random();
        Object lockObj = new Object();
  
        List&lt;Task&lt;int[]&gt;&gt; tasks = new List&lt;Task&lt;int[]&gt;&gt;();
        TaskFactory factory = new TaskFactory(token);
        for (int taskCtr = 0; taskCtr &lt;= 10; taskCtr++) {
           int iteration = taskCtr + 1;
           tasks.Add(factory.StartNew( () =&gt; {
                                         int value;
                                         int[] values = new int[10];
                                         for (int ctr = 1; ctr &lt;= 10; ctr++) {
                                            lock (lockObj) {
                                               value = rnd.Next(0,101);
                                            }
                                            if (value == 0) { 
                                               source.Cancel();
                                               Console.WriteLine("Cancelling at task {0}", iteration);
                                               break;
                                            }   
                                            values[ctr-1] = value; 
                                         }
                                         return values;
                                      }, token));   
  
        }
        try {
           Task&lt;double&gt; fTask = factory.ContinueWhenAll(tasks.ToArray(), 
                                                        (results) =&gt; {
                                                           Console.WriteLine("Calculating overall mean...");
                                                           long sum = 0;
                                                           int n = 0; 
                                                           foreach (var t in results) {
                                                              foreach (var r in t.Result) {
                                                                    sum += r;
                                                                    n++;
                                                                 }
                                                           }
                                                           return sum/(double) n;
                                                        } , token);
           Console.WriteLine("The mean is {0}.", fTask.Result);
        }   
        catch (AggregateException ae) {
           foreach (Exception e in ae.InnerExceptions) {
              if (e is TaskCanceledException)
                 Console.WriteLine("Unable to compute mean: {0}", 
                                   ((TaskCanceledException) e).Message);
              else
                 Console.WriteLine("Exception: " + e.GetType().Name);
           }
        }
        finally {
           source.Dispose();
        }
     }
  }
  </code></pre>