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