title: c# - Trying to Dispose task while it is executing code
body: <p>While "worker" is executing piece of code, I'm closing the whole window and I want to dispose it on closing that window because it is finishing it's code otherwise.</p>
  
  <pre><code>Task  worker = Task.Factory.StartNew(new Action(() =&gt;
  { 
      // some code here
  }
  </code></pre>
  
  <p>Unfortunetly, when I call <code>worker.Dispose()</code> in <code>Close()</code> there is an Exception: </p>
  
  <blockquote>
    <p>A task may only be disposed if it is in a completion state
    (RanToCompletion, Faulted or Canceled)</p>
  </blockquote>
  
  <p>Any suggestions how I can dispose it while it is working?</p>