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.