.NET 4.0 的 Task 类 - Promise 接口?
Task
类是否有像 jQuery 的 deferred 的 promise
方法一样的 Promise 接口?
Is there a promise interface for the Task
class like jQuery's deferred's promise
method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
TPL 和Task 类与jQuery 的承诺有很大不同。
Task
实际上更像是原始操作。如果您希望在任务完成时运行某些内容,则可以在任务上使用延续。这实际上看起来更像是:如果您想继续执行多个任务,可以使用
Task.Factory。
ContinueWhenAll
或Task.Factory。
ContinueWhenAny
到进行适用于多个任务的延续。The TPL, and the Task class, are very different than jQuery's promise.
A
Task
is actually effectively more like the original action. If you wanted to have something run when the task completed, you'd use a continuation on the Task. This would effectively look more like:If you want to continue on multiple tasks, you can use
Task.Factory.
ContinueWhenAll
orTask.Factory.
ContinueWhenAny
to make continuations that works on multiple tasks.看来您正在寻找 TaskCompletionSource:
代码取自此处: 何时应该使用 TaskCompletionSource< ;T>会被使用吗?
It seems like you're looking for TaskCompletionSource:
The code is taken from here: When should TaskCompletionSource<T> be used?
这听起来像是一个延续,所以使用
.ContinueWith(callback)
;或者在 C# 5.0 中,只需await
,即不同的 API,但我认为它可以满足您的要求(有点难以确定......我不是< em>完全确信我理解这个问题)
That sounds like a continuation, so use
.ContinueWith(callback)
; or in C# 5.0, simplyawait
, i.e.different API, but I think it does what you are asking (a little hard to be sure... I'm not entirely sure I understand the question)