title: C# A task was cancelled
body: <p>I wrote a static class which tests the internet connectivity of the user, and raises an event if their connection status changes:</p>
  
  <pre><code>class InternetConnectionMonitor
  {
  
      public static EventHandler&lt;EventArgs&lt;bool&gt;&gt; StatusChanged;
  
      private static bool isCancelled;
      private static bool isConnected;
  
      private static bool IsConnected
      {
          get
          {
              return isConnected;
          }
          set
          {
              if (isConnected != value)
              {
                  StatusChanged(null, new EventArgs&lt;bool&gt;(value));
              }
              isConnected = value;
          }
      }
  
      public static async void Start(TimeSpan interval)
      {
          //TODO Use a 1st party webpage for connectivity testing.
          using (var client = new HttpClient())
          {
              client.Timeout = TimeSpan.FromSeconds(2);
              while (!isCancelled)
              {
                  try
                  {
                      await client.GetAsync("http://example.com");
                      IsConnected = true;
                  }
                  catch (Exception)
                  {
                      IsConnected = false;
                  }
                  await Task.Delay(interval);
              }
          }
      }
  
      public static void Stop()
      {
          isCancelled = true;
      }
  }
  </code></pre>
  
  <p>This class works wonderfully, however, when doing some other processing using the TPL Dataflow in my app, an exception is raised in the while loop of the Start() method saying the task was cancelled.  The reason I am posting here, is because I never cancel any tasks.</p>
  
  <p>Here is the processing I am doing.  The task cancelled exception is raised in InternetConnectionMonitor after QueueTests is completed, although QueueTests makes no reference to InternetConnectionMonitor whatsoever.</p>
  
  <p>If I don't call validateProxies(), the exception is never raised.</p>
  
  <pre><code>private async void validateProxies(IEnumerable&lt;Proxy&gt; proxies)
  {
      validateProxiesButton.Enabled = false;
      cancelValidatingProxiesButton.Enabled = true;
      addProxiesButton.Enabled = false;
      removeProxiesButton.Enabled = false;
      proxyTester = new ProxyTester();
      await proxyTester.QueueTests(proxies, judges);
      validateProxiesButton.Enabled = true;
      cancelValidatingProxiesButton.Enabled = false;
      addProxiesButton.Enabled = true;
      removeProxiesButton.Enabled = true;
      MessageBox.Show("Complete!");
  }
  
  public class ProxyTester
  {
  
      private PauseOrCancelTokenSource pcts = new PauseOrCancelTokenSource();
  
      public async Task QueueTests(IEnumerable&lt;Proxy&gt; proxies, IEnumerable&lt;ProxyJudge&gt; judges, int maxConcurrency = 100)
      {
          var testProxies = new ActionBlock&lt;(Proxy proxy, IProxyTest test)&gt;((tup) =&gt;
          {
              tup.proxy.Status = ProxyStatus.Testing;
              tup.proxy.Status = tup.proxy.TestValidity(tup.test);
          }, new ExecutionDataflowBlockOptions { CancellationToken = pcts.Token.CancellationToken, MaxDegreeOfParallelism = maxConcurrency });
  
          //Set each proxies status to Queued, and post to the dataflow block.
          foreach (var proxy in proxies)
          {
              proxy.Status = ProxyStatus.Queued;
              await testProxies.SendAsync((proxy, judges.GetRandomItem()));
          }
  
          testProxies.Complete();
  
          try
          {
              await testProxies.Completion;
          }
          catch (Exception)
          {
  
          }
      }
  
      public void Cancel()
      {
          pcts.Cancel();
      }
  
  }
  </code></pre>
  
  <p>Starting InternetConnectionMonitor (requested by JleruOHeP in comments)</p>
  
  <pre><code>public proxyTesterView()
  {
      InitializeComponent();
  
      InternetConnectionMonitor.StatusChanged += InternetConnectionMonitor_StatusChanged;
      InternetConnectionMonitor.Start(TimeSpan.FromSeconds(1));
  }
  
  private void InternetConnectionMonitor_StatusChanged(object sender, EventArgs&lt;bool&gt; e)
  {
      if (e.Value == true)
      {
          MessageBox.Show("Online");
      }
      else
      {
          MessageBox.Show("Offline");
      }
  }
  </code></pre>