Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I am trying to avoid any exceptions when cancelling.</p> </blockquote> <p>You shouldn't do that.</p> <p>Throwing <code>OperationCanceledException</code> is the idiomatic way that "the method you called was cancelled" is expressed in TPL. Don't fight against that - just expect it.</p> <p>It's a <em>good</em> thing, because it means that when you've got multiple operations using the same cancellation token, you don't need to pepper your code at every level with checks to see whether or not the method you've just called has actually completed normally or whether it's returned due to cancellation. You <em>could</em> use <a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken.aspx"><code>CancellationToken.IsCancellationRequested</code></a> everywhere, but it'll make your code a lot less elegant in the long run.</p> <p>Note that there are <em>two</em> pieces of code in your example which are throwing an exception - one within the task itself:</p> <pre><code>cancelToken.ThrowIfCancellationRequested() </code></pre> <p>and one where you wait for the task to complete:</p> <pre><code>task.Wait(cancellationToken.Token); </code></pre> <p>I don't think you really want to be passing the cancellation token into the <code>task.Wait</code> call, to be honest... that allows <em>other</em> code to cancel <em>your waiting</em>. Given that you know you've just cancelled that token, it's pointless - it's <em>bound</em> to throw an exception, whether the task has actually noticed the cancellation yet or not. Options:</p> <ul> <li>Use a <em>different</em> cancellation token (so that other code can cancel your wait independently)</li> <li>Use a time-out</li> <li>Just wait for as long as it takes</li> </ul>
    singulars
    1. This table or related slice is empty.
    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. 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