title: When to dispose CancellationTokenSource?
body: <p>The class <code>CancellationTokenSource</code> is disposable. A quick look in Reflector proves usage of <code>KernelEvent</code>, a (very likely) unmanaged resource.
  Since <code>CancellationTokenSource</code> has no finalizer, if we do not dispose it, the GC won't do it.</p>
  
  <p>On the other hand, if you look at the samples listed on the MSDN article <a href="http://msdn.microsoft.com/en-us/library/dd997364.aspx" rel="noreferrer">Cancellation in Managed Threads</a>, only one code snippet disposes of the token.</p>
  
  <p>What is the proper way to dispose of it in code?</p>
  
  <ol>
  <li>You cannot wrap code starting your parallel task with <code>using</code> if you do not wait for it. And it makes sense to have cancellation only if you do not wait.</li>
  <li>Of course you can add <code>ContinueWith</code> on task with a <code>Dispose</code> call, but is that the way to go?</li>
  <li>What about cancelable PLINQ queries, which do not synchronize back, but just do something at the end? Let's say <code>.ForAll(x =&gt; Console.Write(x))</code>?</li>
  <li>Is it reusable? Can the same token be used for several calls and then dispose it together with the host component, let's say UI control? </li>
  </ol>
  
  <p>Because it does not have something like a <code>Reset</code> method to clean-up <code>IsCancelRequested</code> and <code>Token</code> field I would suppose it's not reusable, thus every time you start a task (or a PLINQ query) you should create a new one. Is it true? If yes, my question is what is the correct and recommended strategy to deal with <code>Dispose</code> on those many <code>CancellationTokenSource</code> instances?</p>