Associate a CancellationToken with an async method's Task

后端 未结 1 1176
悲哀的现实
悲哀的现实 2021-01-22 22:47

Question: Is there a way to associate a CancellationToken with the Task returned from an async method?

Generally,

相关标签:
1条回答
  • 2021-01-22 22:51

    I think the design works well for the common case: if any child operations are cancelled, then the cancellation propagates to the parent (the most common case is that the parent and child share cancellation tokens).

    If you want different semantics, you can catch the OperationCanceledException in your async method and throw an exception that fits the semantics you need. If you want to use these semantics repeatedly, an extension method for Task should fit the bill.

    0 讨论(0)
提交回复
热议问题