-1

This question (Is there a way to tell which Tasks are currently running in Task Parallel Library?) says that you can access the currently running Task by the Task.Current property. But I can't find any such property in the documentation.

Why is this?

Community
  • 1
  • 1
Petr Hudeček
  • 1,623
  • 19
  • 30
  • 1
    Based on the fact that even the other question states that it does not work and there are answers that do work, this question seems to be pretty pointless. Even if you find it... it won't help you. – nvoigt May 20 '14 at 07:36
  • 1
    As in the Thread said, there is no such Property. You might think about TaskScheduler.Current The question is, what Informations do you need from the Task? – Matthias Müller May 20 '14 at 07:37
  • The answer only states that there is no way to access all running tasks (not just one) and neither the answer nor the comments contradict the OP's claim that the Task.Current property exists so I thought maybe I missed something. – Petr Hudeček May 20 '14 at 07:41
  • And the utility of it might be that I could schedule a continuation task to run right after I finish. – Petr Hudeček May 20 '14 at 07:42

1 Answers1

1

What would you want Task.Current to be, had it been there? Consider this:

async Task DoAsync()
{
    var currentTask = Task.Current; // imagine Task.Current does exist

    await Task.Delay(1000);
}

// ...

var task = Task.Run(() => DoAsync());

Would you want currentTask to be the task returned by Task.Run or by DoAsync?

Perhaps, a less contrived question would be this:

How to access the Task object from the task's own action?

You certainly cannot do it the following way, as it has a race condition:

Task task = null;
task = Task.Run(() => Console.WriteLine({ task.Id }));

You can however do it this way:

Task task = null;
task = new Task(() => Console.WriteLine({ task.Id }));
task.Run();

Is it what you're looking for? In my experience, I only once needed something like this:

Task sequencing and re-entracy

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486