Why CancellationToken is separate from CancellationTokenSource?

后端 未结 5 1303
夕颜
夕颜 2020-12-02 06:22

I\'m looking for a rationale of why .NET CancellationToken struct was introduced in addition to CancellationTokenSource class. I understand how

5条回答
  •  北海茫月
    2020-12-02 07:04

    The CancellationToken is a struct so many copies could exist due to passing it along to methods.

    The CancellationTokenSource sets the state of ALL copies of a token when calling Cancel on the source. See this MSDN page

    The reason for the design might be just a matter of separation of concerns and the speed of a struct.

提交回复
热议问题