如何创建任务<>我可以手动完成 [英] How to create a Task<> I can complete manually

查看:137
本文介绍了如何创建任务<>我可以手动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单元测试组件我需要验证的组件如何响应在不同的时间被完成的任务。

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 resolve at will?

推荐答案

您可以使用的 TaskCompletionSource 来创建一个完全手动的任务。

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.

把手伸到完成源的任务物业给消费者,并调用的setResult 就可以了(随意),以完成任务。请注意,你也有 SetCanceled SetException 来表示取消和失败,分别为。

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.

这篇关于如何创建任务&LT;&GT;我可以手动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆