Code Monkey home page Code Monkey logo

treeifytask's Introduction

TreeifyTask (formerly Octopus.TaskTree)

Release to Nuget NuGet Downloads

Dotnet component that helps you to manage async tasks in a hierarchical fashion.

There are situations that we might have to manage tasks in a tree like structure. Means, the parent task depends on child tasks to be completed in a serial or concurrent fashion. The parent task needs to show the average progress value of overall operation.

This component ITaskNode lets you to Create a task, Set a custom asynchronous function Func<IProgressReporter, CancellationToken, Task> for it, Create one or more child tasks and get overall progress.

You have several options that you can execute them concurrently or in series. The overall progress will be updated to parent task.

Please see below code snippet for the better understanding.

Create structured 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);

Set actions

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...");
});

Subscribe to reporting event to get overall progress

// 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
};

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);

Demo execution in WPF application

Series execution with cancellation

octopus-tasktree-series-exec-with-cancel

Concurrent execution successful

octopus-tasktree-concur-exec

Concurrent execution with cancellation

octopus-tasktree-concur-exec-with-cancel

Please note: If rootTask has 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.

Road map

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:

var rootTask =
    new TaskNode("Root", null,
        new TaskNode("Task1", Task1_Action,
            new TaskNode("Task1.1", Task1_1_Action),
            new TaskNode("Task1.2", Task1_2_Action)),
        new TaskNode("Task2", null,
            new TaskNode("Task2.1", null,
                new TaskNode("Task2.1.1", Task2_1_1_Action),
                new TaskNode("Task2.1.2", Task_2_1_2_Action)),
            new TaskNode("Task2.2", Task2_2_Action)),
        new TaskNode("Task3", Task3_Action),
        new TaskNode("Task4", Task4_Action),
        new TaskNode("Task5", Task5_Action),
        new TaskNode("Task6", Task6_Action));


private async Task Task1_Action(IProgressReporter reporter, CancellationToken token)
{
    reporter.ReportProgress(TaskStatus.InProgress, 10, "Started...");
    await Task.Delay(1000);
    reporter.ReportProgress(TaskStatus.InProgress, 40, "InProgress...");
    await Task.Delay(1000);
    reporter.ReportProgress(TaskStatus.Completed, 100, "Done...");
}
  • Find a Child and Set Preferences to it.
var task2_1_2 = rootTask.Find("Task2/Task2.1/Task2.1.2");

// Preferences
task2_1_2.ExecutionMode = ExecutionMode.Series;
task2_1_2.ThrowOnError = false;

rootTask.ExecuteConcurrently(cancellationToken: token, throwOnError: true);

treeifytask's People

Contributors

gokul-e-intuit avatar gokulegit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.