Use Task.FromCanceled<TResult>() on NETSTANDARD1_3#772
Conversation
|
Hi @justinvp, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution! The agreement was validated by .NET Foundation and real humans are currently evaluating your PR. TTYL, DNFBOT; |
|
Not sure what the benefit of this is. I'm inclined not to take it since it only adds code with no clear improvement. |
|
Benefits:
|
|
Another benefit:
|
| throw new ObjectDisposedException(nameof(FrameRequestStream)); | ||
| case FrameStreamState.Aborted: | ||
| return TaskUtilities.GetCancelledZeroTask(); | ||
| return TaskUtilities.GetCancelledZeroTask(default(CancellationToken)); |
There was a problem hiding this comment.
Unrelated to this change, but I noticed that FrameResponseStream.ValidateState only returns a canceled task for the Aborted case when cancellationToken.IsCancellationRequested is true, whereas here it is always returning a canceled task for Aborted regardless of cancellationToken.IsCancellationRequested.
FrameResponseStream.ValidateState (src):
case FrameStreamState.Aborted:
if (cancellationToken.IsCancellationRequested)
{
// Aborted state only throws on write if cancellationToken requests it
return TaskUtilities.GetCancelledTask(cancellationToken);
}
break;Is this by design, or should FrameRequestStream be modified to behave the same as FrameResponseStream?
There was a problem hiding this comment.
Is by design #567 (comment) and the reading and writing are different.
| } | ||
|
|
||
| public static Task<int> GetCancelledZeroTask() | ||
| public static Task<int> GetCancelledZeroTask(CancellationToken cancellationToken) |
There was a problem hiding this comment.
I would add an overload GetCancelledZeroTask() which calls GetCancelledZeroTask(CancellationToken.None). This is cleaner than requiring the caller to pass default(CancellationToken) or CancellationToken.None.
There was a problem hiding this comment.
How about making it an optional parameter with a default value of default(CancellationToken)?
There was a problem hiding this comment.
Sure, either would be fine.
|
@justinvp @mikeharder Yeah, the point about avoiding an allocation made me change my mind 😄 It's sad though that we can't have this on all frameworks. |
|
Sorry for not making the benefits more clear from the start. I'll update the PR, addressing @mikeharder's feedback, later today. |
Task.FromCanceled<int>(cancellationToken) can be used on NETSTANDARD1_3 in TaskUtilities.GetCancelledZeroTask().
|
Feedback addressed. Thanks for the review. |
|
|
Task.FromCanceled<int>(cancellationToken)can be used onNETSTANDARD1_3inTaskUtilities.GetCancelledZeroTask().