2

I have an external long running process (web API call) which I have to track in order to know when it completes. I'd like to encapsulate it in a task. TaskCompletionSource seems to be the right tool to achieve that. But the task associated to TaskCompletionSource has never the running status. It starts with the WaitingForActivation status and get the RanToCompletion status once the result has been set. In order to include this inside a more general frame, I need to know that the underlying long process is running. In this case, the task should have the Running status. I'm wondering how I can get/force/simulate this behavior.

And above this practical question, I don't understand the design choice of bypassing the "running" status with TaskCompletionSource. We could have been allowed to use the Start method to set the status to "Running". It seems simple and straighforward. But calling "Start" throw an exception. I don't understand this choice.

Hope it's clear. Thanks in advance.

Patrice
  • 29
  • 3
  • How about the [status](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.taskstatus) `WaitingToRun`, meaning *"the task has been scheduled for execution but has not yet begun executing"*? Do you want to simulate it as well? If not, then the property `IsCompleted` should be sufficient to determine if the task is running or not. – Theodor Zoulias Apr 29 '21 at 05:33
  • After all, it's probably simpler to create a normal task and embed my tracking logic inside... – Patrice Apr 29 '21 at 05:38
  • 1
    @TheodorZoulias Thank you for your comment. Yes Ideally I'd like to simulate it as well. But it's a "nice to have". The only status I really need is "Running". You are right, I can logically know if the task is running but, as I said, this task has to be integrated with a more general framework with other "normal" tasks and it relies on their status. That's why I want the TaskCompletionSource.Task behaves like others. – Patrice Apr 29 '21 at 05:55
  • So you would probably like if the `TaskCompletionSource` had a method `SetStatus`, that you could use to control precisely the status of the `Task`. Obviously this method does not exist, but it may be possible to create one that uses reflection to change the internal state of the object. It would be a highly risky and fragile solution though. – Theodor Zoulias Apr 29 '21 at 06:02
  • You are right. It seems very tricky and risky. And before doing such acrobatic things, I thindk I should understand why MS has designed this tool this way. And I don't. – Patrice Apr 29 '21 at 07:22
  • 1
    The right way to implement a `Task` object that will transition through the `Running` state before reaching the `RanToCompletion` state is simply to use `Task.Run()` to run your task. _"I need to know that the underlying long process in running"_ -- why? how is that useful in your scenario? You seem to have an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) here. You have some _other_ goal you are trying to achieve, but instead of asking about that, you are asking about the not-feasible solution you prematurely decided on. – Peter Duniho Apr 29 '21 at 07:38
  • If you can't put your task in an actual `Task` (and if not, why?), then you can keep track of whether it's "running" or not some other way. Just keep a flag somewhere, for example. There's no reason that `TaskCompletionSource` has to do all the work. If you still think you need help, fix your post so that it includes a proper [mcve] that shows the scenario clearly, and explain in detail all the constraints that led you to the unwise idea of a `TaskCompletionSource` that could be in the `Running` state. – Peter Duniho Apr 29 '21 at 07:39
  • 2
    I found two related questions: [Is there a way of setting a Task driven via TaskCompletionSource to Status 'Running'?](https://stackoverflow.com/questions/14836995/is-there-a-way-of-setting-a-task-driven-via-taskcompletionsource-to-status-runn) and [Async always WaitingForActivation](https://stackoverflow.com/questions/20830998/async-always-waitingforactivation). There are [two types](https://blog.stephencleary.com/2014/04/a-tour-of-task-part-0-overview.html) of tasks, delegate-based tasks and promise-style tasks. The `TaskCompletionSource.Task` is a typical example of the second type. – Theodor Zoulias Apr 29 '21 at 08:13
  • Not sure why this question was flagged as unclear. There's nothing mysterious about the X here, even if there are better approaches to Y than what's mentioned. – William Feb 28 '22 at 17:24

0 Answers0