.net core - Passing an unknown number of IProgress<T> to class library

.net core - Passing an unknown number of IProgress<T> to class library

I have a console app which uses a class library to execute some long running tasks. This is a .net core console app and uses the .net core Generic Host. I also use the&nbsp;<a href="https://github.com/Mpdreamz/shellprogressbar" target="_blank">ShellProgressBar</a>&nbsp;library to display some progress bars.

I have a console app which uses a class library to execute some long running tasks. This is a .net core console app and uses the .net core Generic Host. I also use the ShellProgressBar library to display some progress bars.

My Hosted service looks like this

internal class MyHostedService : IHostedService, IDisposable
{
    private readonly ILogger _logger;
    private readonly IMyService _myService;
    private readonly IProgress<MyCustomProgress> _progress;
    private readonly IApplicationLifetime _appLifetime;
    private readonly ProgressBar _progressBar;
    private readonly IProgressBarFactory _progressBarFactory;

public MyHostedService(
    ILogger&lt;MyHostedService&gt; logger, 
    IMyService myService,
    IProgressBarFactory progressBarFactory,
    IApplicationLifetime appLifetime)
{
    _logger = logger;
    _myService = myService;
    _appLifetime = appLifetime;
    _progressBarFactory = progressBarFactory;

    _progressBar = _progressBarFactory.GetProgressBar();        // this just returns an instance of ShellProgressBar

    _progress = new Progress&lt;MyCustomProgress&gt;(progress =&gt;
    {
        _progressBar.Tick(progress.Current);
    });
}

public void Dispose()
{
    _progressBar.Dispose();
}

public Task StartAsync(CancellationToken cancellationToken)
{
    _myService.RunJobs(_progress);
    _appLifetime.StopApplication();

    return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
    return Task.CompletedTask;
}

}

Where MyCustomProgress looks like this

public class MyCustomProgress
{
    public int Current {get; set;}
    public int Total {get; set;}
}

and MyService looks something like so (Job1Job2Job3 implement IJob)

public class MyService : IMyService
{
    private void List<IJob> _jobsToRun;

public MyService()
{
    _jobsToRun.Add(new Job1());
    _jobsToRun.Add(new Job2());
    _jobsToRun.Add(new Job3());
}

public void RunJobs(IProgress&lt;MyCustomProgress&gt; progress)
{           
    _jobsToRun.ForEach(job =&gt; 
    {
        job.Execute();

        progress.Report(new MyCustomProgress { Current = _jobsToRun.IndexOf(job) + 1, Total = _jobsToRun.Count() });
    });
}

}

And IJob is

public interface IJob
{
    void Execute();
}

This setup works well and I'm able to display the progress bar from my HostedService by creating a ShellProgressBar instance and using the one IProgress instance I have to update it.

However, I have another implementation of IMyService that I also need to run that looks something like this

public class MyService2 : IMyService
{
    private void List<IJob> _sequentialJobsToRun;
    private void List<IJob> _parallelJobsToRun;

public MyService()
{
    _sequentialJobsToRun.Add(new Job1());
    _sequentialJobsToRun.Add(new Job2());
    _sequentialJobsToRun.Add(new Job3());


    _parallelJobsToRun.Add(new Job4());
    _parallelJobsToRun.Add(new Job5());
    _parallelJobsToRun.Add(new Job6());
}

public void RunJobs(IProgress&lt;MyCustomProgress&gt; progress)
{       
    _sequentialJobsToRun.ForEach(job =&gt; 
    {
        job.Execute();

        progress.Report(new MyCustomProgress { Current = _jobsToRun.IndexOf(job) + 1, Total = _jobsToRun.Count() });
    });

    Parallel.ForEach(_parallelJobsToRun, job =&gt; 
    {
        job.Execute();

        // Report progress here
    });
}

}

This is the one I'm struggling with. when _parallelJobsToRun is executed, I need to be able to create a new child ShellProgressBar (ShellProgressBar.Spawn) and display them as child progress bars of let's say 'Parallel Jobs'.

This is where I'm looking for some help as to how I can achieve this.

Note: I don't want to take a dependency on ShellProgressBar in my class library containing MyService

Any help much appreciated.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Add asp.net Identity in empty project

LIKE | COMMENT | SHARE | SUBSCRIBE Today, in this tutorial you will see the process of adding an ASP.NET Identity to the ASP.NET Web Application developed in...

List all users from asp net core identity

LIKE | COMMENT | SHARE | SUBSCRIBE In this video, i will show you How to retrieve and display all the registered application users in asp.net core using the ...

Extend IdentityUser in ASP NET Core

LIKE | COMMENT | SHARE | SUBSCRIBE In this tutorials, I will show you how to extend IdentityUser in asp.net core.. Subscribe & Stay Connected! Thank You! ♥ #...