Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason for this is simple, you are calling <a href="http://msdn.microsoft.com/en-us/library/system.idisposable.dispose.aspx" rel="nofollow"><code>Dispose</code></a> on the continuation itself and <em>not</em> on the first task</p> <p>Your code consists of:</p> <pre><code>Task task = null; var task = &lt;task 1&gt;.ContinueWith(t =&gt; { /* task 2 */ task.Dispose(); }); </code></pre> <p>In the above code, <code>task</code> is equal to the continuation (<a href="http://msdn.microsoft.com/en-us/library/dd270696.aspx" rel="nofollow"><code>ContinueWith</code></a> doesn't pass back the original <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx" rel="nofollow"><code>Task</code></a>, it passes the continuation) and that's what's getting captured in the closure you're passing to <code>ContinueWith</code>.</p> <p>You can test this by comparing the references of the <code>Task</code> parameter passed into the <code>ContinueWith</code> method with <code>task</code>:</p> <pre><code>Task task = null; var task = &lt;task 1&gt;.ContinueWith(t =&gt; { /* task 2 */ if (object.ReferenceEquals(t, task)) throw new InvalidOperationException("Trying to dispose of myself!"); task.Dispose(); }); </code></pre> <p>In order to dispose of the first, you need to break it up into two <code>Task</code> variables and capture the first <code>Task</code>, like so:</p> <pre><code>var task1 = &lt;task 1&gt;; var task2 = task1.ContinueWith(t =&gt; { // Dispose of task1 when done. using (task1) { // Do task 2. } }); </code></pre> <p>However, because the <code>previous Task</code> is passed to you as a parameter in the <code>ContinueWith</code> method, you don't need to capture <code>task</code> in the closure at all, you can simply call <code>Dispose</code> on the <code>Task</code> passed as a parameter to you:</p> <pre><code>var task = &lt;task 1&gt;.ContinueWith(t =&gt; { // t = task 1 // task = task 2 // Dispose of task 1 when done. using (t) { // Do task 2. } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload