Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way of setting a Task driven via TaskCompletionSource to Status 'Running'?

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?

like image 938
mackenir Avatar asked Feb 12 '13 16:02

mackenir


People also ask

What is the use of TaskCompletionSource?

Run turns something synchronous into a Task (by running it on a separate thread), TaskCompletionSource turns something that is already asynchronous into a Task . "If it is already asynchronous, why does it need to be turned into a Task ?"

How do I run a task in C#?

To start a task in C#, follow any of the below given ways. Use a delegate to start a task. Task t = new Task(delegate { PrintMessage(); }); t. Start();

Is TaskCompletionSource thread safe?

Is it safe to pass non-thread-safe objects created on one thread to another using TaskCompletionSource. SetResult()? Yes, as long as the object can be used on a different thread than the one it was created on (of course).


1 Answers

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".

like image 185
Andrew Arnott Avatar answered Feb 11 '23 03:02

Andrew Arnott