title: Cancel long running Task after 5 seconds when not finished
body: <p>I have created a task which creates an XML string. The task can last multiple seconds. When the task isn't finished after 5 seconds, I want to cancel the Task 'smootly' and continue with writing the rest of the XML. So I built in cancellation inside my task. But although I see the following message in the Log:</p>
  
  <ul>
  <li>ProcessInformationTask timed out</li>
  </ul>
  
  <p>I also see this line in my log</p>
  
  <p>Adding the process information took 10001 ms </p>
  
  <p>I wonder why this can happen because I want to cancel the Task after 5 seconds (if not finished). So I expect the task to last 5 seconds max. How can I solve this? Probably the cancelation isn't setup properly?</p>
  
  <p><em>Code where I call my task</em></p>
  
  <pre><code>string additionalInformation = null;
  var contextInfo = new StringBuilder();
  
  var xmlWriterSettings = new XmlWriterSettings()
  {
      OmitXmlDeclaration = true,
      ConformanceLevel = ConformanceLevel.Fragment
  };
  
  var tokenSource = new CancellationTokenSource();
  var token = tokenSource.Token;
  
  using (XmlWriter xmlWriter = XmlWriter.Create(contextInfo, xmlWriterSettings))
  {
      try
      {
          xmlWriter.WriteStartElement("AdditionalInformation");
  
          //Write xml (not long running)
  
          var watch = System.Diagnostics.Stopwatch.StartNew();
          string processInformation = AddProcessesInformation(xmlWriterSettings);
          watch.Stop();
          var elapsedMs = watch.ElapsedMilliseconds;
          Log.Info("Adding the process information took : " + elapsedMs + " ms");
  
          if (!string.IsNullOrEmpty(processInformation))
          {
              xmlWriter.WriteRaw(processInformation);
          }
  
          //Write xml (not long running)
  
          xmlWriter.WriteEndElement();
  
          additionalInformation = contextInfo.ToString();
      }
  
      catch (Exception e)
      {
          Log.Info("An exception occured during writing the additional information: " + e.Message);
          return false;
      }
  
      return true;
  } 
  </code></pre>
  
  <p>Task method</p>
  
  <pre><code>private static string AddProcessesInformation(XmlWriterSettings xmlWriterSettings)
      {
          var tokenSource = new CancellationTokenSource();
          var token = tokenSource.Token;
          var contextInfo = new StringBuilder();
  
          var processInformationTask = Task&lt;string&gt;.Factory.StartNew(() =&gt;
          {
              if (token.IsCancellationRequested)
              {
                  Log.Info("Cancellation request for the ProcessInformationTask");
              }
  
              Thread.Sleep(10000);
  
              return "Ran without problems'";
  
          }, token);
  
          if (!processInformationTask.Wait(5000, token))
          {
              Log.Info("ProcessInformationTask timed out");
              tokenSource.Cancel();
          }
  
          return processInformationTask?.Result;
      }
  </code></pre>