How to cancel a task waiting on UniTaskCompletionSource.Task? #628
Unanswered
f1yingbanana
asked this question in
Q&A
Replies: 1 comment
-
You don’t “unsubscribe” by cancelling the source. You cancel your own await. Use a CancellationToken on the awaiting side and attach it to the task. That cancels only your waiter and leaves the source + other waiters untouched:
Unsubscribing becomes just cts.Cancel():
If you’re using UniTaskCompletionSource as an event/notification, make it single-shot and replace it after firing:
(Alternative for stream-style events: use AsyncReactiveProperty/IUniTaskAsyncEnumerable/Channel, but for simple “next signal” the pattern above is enough.) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
It's neat that with UniTaskCompletionSource we are able to wait on the Task multiple times, and I'm kind of using it as an event/notification system. The problem is, I can't figure out how to cancel or "unsubscribe" from such a source. It looks like the standard C# way is to just cancel the TaskCompletionSource instead, but that would mean sending a canceled message to all the other waiters.
Is there a way to cancel this or am I just using it wrong?
Beta Was this translation helpful? Give feedback.
All reactions