Try TreeifyTask
GithubLinkHere
You can create root and sub tasks those can be executed in an order sequentially or in parallel. The handle is from the root node.
Usage: Create root task and child tasks
ITaskNode rootTask = new TaskNode("root");
ITaskNode childTask_1 = new TaskNode("Task-1");
ITaskNode childTask_2 = new TaskNode("Task-2");
rootTask.AddChild(childTask_1);
rootTask.AddChild(childTask_2);
Usage: Set actions to child tasks
childTask_1.SetAction(async (reporter, cancellationToken) => {
// Simple delay function.
reporter.ReportProgress(TaskStatus.InProgress, 10, "Started...");
await Task.Delay(1000);
reporter.ReportProgress(TaskStatus.InProgress, 100, "Finished...");
});
childTask_2.SetAction(async (reporter, cancellationToken) => {
// Simple delay function.
reporter.ReportProgress(TaskStatus.InProgress, 5, "Started...");
await Task.Delay(2500);
reporter.ReportProgress(TaskStatus.InProgress, 100, "Finished...");
});
Usage: Subscribe to Root node
// Before starting the execution, you need to subscribe for progress report.
rootTask.Reporting += (object sender, ProgressReportingEventArgs eventArgs) => {
eventArgs.ProgressValue; // -> this will represent the overall progress
};
Usage: Start execution
// Create and pass the cancellation token
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
// Start the execution concurrently
rootTask.ExecuteConcurrently(cancellationToken: token, throwOnError: true);
// OR
// Start the execution in series
rootTask.ExecuteInSeries(cancellationToken: token, throwOnError: true);
Error handling is easy as Execute*
methods throw exception with entire tree structure. Here the ID
plays a role and it helps to to debug quickly.