Calling any of the
TrySet...
methods on a
TaskCompletionSource<TResult>
which is already in a final state won't cause any problems. That's what the
TrySet...
methods are there for. :)
If the source task accepts a
CancellationToken
, you could alternatively use the
CancellationTokenSource.CancelAfter method[
^] to cancel the task after a specific length of time:
var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(5));
await DoSomethingAsync(cts.Token);