Having instantiated one or more Task
objects in C#, like this for example:
var tasks = new List<Task>
{
Task.Factory.StartNew(MyWorker.DoWork),
Task.Factory.StartNew(AnotherWorker.DoOtherWork)
};
Is there a way to get the Action
method from the task object? In other words, can I return the Action
of MyWorker.DoWork
from its task?
I'm trying to be able to log the status of each task, like this:
Task.WaitAll(tasks.ToArray(), new TimeSpan(0, 0, 1));
var msg = tasks.Aggregate(string.Empty,
(current, task) =>
current + $"{task.Action}: {task.Status}{Environment.NewLine}");
The string value of msg
would be:
MyWorker.DoWork RanToCompletion
AnotherWorker.DoOtherWork Running
(The {task.Action}
portion of my sample doesn't exist and wouldn't compile, of course)