Note that there are some explanatory texts on larger screens.

plurals
  1. POC# async CTP - How do I mark an async task as canceled without throwing a TaskCanceledException?
    primarykey
    data
    text
    <p>I have a short async task that will frequently need to be canceled after it has started. The "Task" class has an IsCanceled indicator which I think would be convenient to use to indicate that the async task was canceled without running to completion, but as far as I can tell the only way to have an async Task be marked as canceled is to throw a TaskCanceledException in the async function. Throwing an exception routinely, to indicate a situation which occurs unexceptionally, goes against how I understand exceptions should be used. Does anyone know a better way to indicate an async task to be canceled when it is expected to happen frequently? </p> <p>My next-best alternative is to return a structure that has it's own IsCanceled property:</p> <p>(I've ignored some good coding and style practices for brevity here)</p> <pre><code>class MightBeCanceled&lt;T&gt; { public readonly T Value; public readonly bool IsCanceled; public MightBeCanceled(T value) { Value = value; IsCanceled = false; } public static MightBeCanceled&lt;T&gt; Canceled = new MightBeCanceled&lt;T&gt;(default(T), true); private MightBeCanceled(T value, bool isCanceled) { Value = value; IsCanceled = isCanceled; } } ... static async Task&lt;MightBeCanceled&lt;int&gt;&gt; Foo() { if (someCancellationCondition) return MightBeCanceled&lt;int&gt;.Canceled; else return new MightBeCanceled&lt;int&gt;(42); } static async void Bar() { var mightBeCanceled = await Foo(); if (mightBeCanceled.IsCanceled) ; // Take canceled action else ; // Take normal action } </code></pre> <p>But this seems redundant and harder to use. Not to mention it introduces consistency problems because there will be two IsCanceled's (one in the Task and one in the MightBeCanceled).</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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