Suppose I want to ensure that a task object which is returned from an asynchronous method will transition into the canceled state, due to a cancellation request by the caller.
Catch is: this should occur regardless of how the asynchronous methods consumed by the aforementioned method are implemented and whether or not they ever complete.
Consider the following extension method:
public static Task<T> ToTask<T>(this CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<T>();
cancellationToken.Register(() => { tcs.SetCanceled(); });
return tcs.Task;
}
I can now consume such a task to ensure said scenario:
public async Task<Item> ProvideItemAsync(CancellationToken cancellationToken)
{
Task<Item> cancellationTask = cancellationToken.ToTask<Item>();
Task<Item> itemTask = _itemProvider.ProvideItemAsync(cancellationToken);
Task<Task<Item>> compoundTask = Task.WhenAny(cancellationTask, itemTask);
Task<Item> finishedTask = await compoundTask;
return await finishedTask;
}
My questions are:
1) Are there any issues with this approach?
2) Is there a built in API to facilitate such a use case
Thanks!