Is there a way of setting a Task driven via TaskCo

2019-04-29 17:10发布

问题:

I'm using TaskCompletionSource to provide and drive an instance of Task. I would like to be able to set the Task to status Running to indicate that the task is... 'running' however I can't see a way to achieve this via TaskCompletionSource.

Is there a way to do this?

回答1:

No. There isn't a way.

Whether or not you like my answer, however, it is the correct one. :-)

The following is my opinion, and an attempt to help you feel better.

Task.Status has several states that are only set and useful when the task is a scheduled task. Tasks from TaskCompletionSource are not scheduled tasks. The concept of WaitingToRun, Running, etc. are therefore not applicable in the traditional scheduled task sense. If you did have the ability to set these, you'd have to decide on what semantics to apply to these values, which may conflict with how others interpret them.

Ultimately, I don't think your code should ever make any decisions based on these intermediate states anyway. Doing so sounds like a "code smell".