How to create a Task<> I can complete manually

后端 未结 1 1637
悲&欢浪女
悲&欢浪女 2021-02-18 15:33

In unit testing a component I need to verify how a component reacts to Tasks being completed at various times.

How do I create a Task<> that I can res

相关标签:
1条回答
  • 2021-02-18 16:14

    You can use a TaskCompletionSource to create a fully 'manual' task.

    Represents the producer side of a Task unbound to a delegate, providing access to the consumer side through the Task property.

    Hand out the the completion source's Task property to the consumer, and call SetResult on it (at will) to complete the task. Note that you also have SetCanceled and SetException to represent cancellations and failures, respectively.

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