TreeifyTask - A DotNet library to efficiently manage a mix of synchronous and asynchronous tasks in a hierarchical fashion
A DotNet component that helps C# application developers to manage synchronous and asynchronous tasks in a custom hierarchical fashion. For example, a parent task depends on multiple child tasks to be completed in a sequence or parallel mode. Also, along with the execution, the parent task should be able to access the progress information of all its child tasks.
With this library, ITaskNode lets you to Create a task, set up a custom asynchronous function Func<IProgressReporter, CancellationToken, Task> for it. It allows to create one or more child tasks and get overall progress.
Please see below code snippet for the better understanding.
Create structured Tasks
ITaskNoderootTask=newTaskNode("root");ITaskNodechildTask_1=newTaskNode("Task-1");ITaskNodechildTask_2=newTaskNode("Task-2");rootTask.AddChild(childTask_1);rootTask.AddChild(childTask_2);Set actions
childTask_1.SetAction(async(reporter,cancellationToken)=>{// Simple delay function.reporter.ReportProgress(TaskStatus.InProgress,10,"Started...");awaitTask.Delay(1000);reporter.ReportProgress(TaskStatus.InProgress,100,"Finished...");});childTask_2.SetAction(async(reporter,cancellationToken)=>{// Simple delay function.reporter.ReportProgress(TaskStatus.InProgress,5,"Started...");awaitTask.Delay(2500);reporter.ReportProgress(TaskStatus.InProgress,100,"Finished...");});Subscribe to reporting event to get overall progress
// Before starting the execution, you need to subscribe for progress report.rootTask.Reporting+=(objectsender,ProgressReportingEventArgseventArgs)=>{eventArgs.ProgressValue;// -> this will represent the overall progress};Start execution
// Create and pass the cancellation tokenvartokenSource=newCancellationTokenSource();vartoken=tokenSource.Token;// Start the execution concurrentlyrootTask.ExecuteConcurrently(cancellationToken:token,throwOnError:true);// OR// Start the execution in seriesrootTask.ExecuteInSeries(cancellationToken:token,throwOnError:true);Please note: If
rootTaskhas action set to it then the action will run after executing all child tasks in case of series execution. However in concurrent execution,rootTask's action will also be executed along with child tasks and gets the overall progress.
This is the initial version of the component. Please raise issues if you find any. Comments, suggestions and contributions are always welcome.
Here's the list of items in backlog for the upcoming releases:
- Core Component
- Wpf Sample
- Blazor Server Sample
- Syntax Enhancement on subtasks creation and settings
- Json/Xml config loader
- Extend to Java, Python
Sample tree below:
varrootTask=newTaskNode("Root",null,newTaskNode("Task1",Task1_Action,newTaskNode("Task1.1",Task1_1_Action),newTaskNode("Task1.2",Task1_2_Action)),newTaskNode("Task2",null,newTaskNode("Task2.1",null,newTaskNode("Task2.1.1",Task2_1_1_Action),newTaskNode("Task2.1.2",Task_2_1_2_Action)),newTaskNode("Task2.2",Task2_2_Action)),newTaskNode("Task3",Task3_Action),newTaskNode("Task4",Task4_Action),newTaskNode("Task5",Task5_Action),newTaskNode("Task6",Task6_Action));privateasyncTaskTask1_Action(IProgressReporterreporter,CancellationTokentoken){reporter.ReportProgress(TaskStatus.InProgress,10,"Started...");awaitTask.Delay(1000);reporter.ReportProgress(TaskStatus.InProgress,40,"InProgress...");awaitTask.Delay(1000);reporter.ReportProgress(TaskStatus.Completed,100,"Done...");}- Find a Child and Set Preferences to it.
vartask2_1_2=rootTask.Find("Task2/Task2.1/Task2.1.2");// Preferencestask2_1_2.ExecutionMode=ExecutionMode.Series;task2_1_2.ThrowOnError=false;rootTask.ExecuteConcurrently(cancellationToken:token,throwOnError:true);