为什么任务对象不可重用? [英] Why are Task objects not reusable?

查看:183
本文介绍了为什么任务对象不可重用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题导致我到另一个更普遍的(并且可能根本的)问题,为什么工作对象不能重复使用



微软没有解释写这句话:




一个任务只可以启动,只运行一次。任何试图安排
A任务第二次将导致异常。




这背后的理由那么明显,它不值得解释?是否有重复设置和启动工作与延续?


解决方案

A 任务是一个代表性的一个的东西在未来的某个时刻可能发生的。它提供指示公开当任务完成后,如果它这样做的手段,什么任务的最终状态是,等。



任务已经完成,当你开始它再次又该 IsCompleted 物业的回报?它没有完整的,但你再次启动它。你是说方应只需要访问最终状态的能力(即它是否被取消了,这是例外,它的结果等)的时间之窗之后开始并完成之前呢?那将是非常混乱。



有其他的目的,有可以代表一个操作被重复,或异步操作,如(间其他选项)的一些集合的IObservable ,它实际上是一个异步的的可能(潜在的)代表N的结果调用特定的授权,依次调用。


This question leads me to another more general (and probably fundamental) question, Why are Task objects not reusable?

Microsoft writes this statement without explanation:

A task may only be started and run only once. Any attempts to schedule a task a second time will result in an exception.

Is the reasoning behind this so obvious that it deserves no explanation? Is there no performance hit for repeatedly setting and starting a Task with a Continuation?

解决方案

A Task is a representation of a single something that might happen at some point in the future. It provides means of indicating publicly when the task is done, if it's done, what the final state of that task was, etc.

After a task has finished and you start it up again what should the IsCompleted property return? It did complete, but you started it up again. Are you saying that the caller should only have the ability to access the final state (i.e. whether it was canceled, it's exceptions, it's Result, etc.) for the window of time after it is started and before it is completed? That would be highly confusing.

There are other objects out there that can represent an operation being repeated, or some collection of asynchronous operations, such as (among other options) IObservable, which is effectively an asynchronous sequence that could (potentially) represent the results of N calls to a particular delegated, called sequentially.

这篇关于为什么任务对象不可重用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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