astrowalker astrowalker - 1 month ago 13
C# Question

How to combine TaskCompletionSource and CancellationTokenSource?

I have such code (simplified here) which awaits finishing task:

var task_completion_source = new TaskCompletionSource<bool>();
observable.Subscribe(b =>
{
if (b)
task_completion_source.SetResult(true);
});
await task_completion_source.Task;


The idea is to subscribe and wait for the
true
in the stream of booleans. This finishes the "task" and I can move on beyond the
await
.

However I would like to cancel -- but not subscription, but awaiting. I would like to pass cancel token (somehow) to
task_completion_source
so when I cancel the token source, the
await
will move on.

How to do it?

Update:
CancellationTokenSource
is external to this code, all I have here is the token from it.

Evk Evk
Answer

If I understand you correctly, you can do it like this:

CancellationToken ct;
ct.Register(() =>
{
     // this callback will be executed when token is cancelled
     task_comletion_source.TrySetCanceled();
});

Note that it will throw an exception on your await, which you have to handle.

Comments