How can I update the main progress bar and child progress bars in async tasks?
I'm using ShellProgressBar found here, ShellProgressBar supports child progress bars. When I run async
tasks, the progress bars do not get updated.
I've tried a few different things to get the progress bars to update, the closest I can get is as follows.
private const int NumberOfProcesses = 4;
private const int TicksPerProcess = 1000;
static async Task Main(string[] args)
{
var processes = new List<Task>();
using (var pbar = new ProgressBar(NumberOfProcesses * TicksPerProcess, "Main Progress bar"))
{
for (int i = 0; i < NumberOfProcesses; i++)
{
Console.WriteLine($"Process: i");
var task = Process(pbar);
processes.Add(task);
}
}
await Task.WhenAll(processes.ToArray());
Console.WriteLine("Press any key to close");
Console.ReadKey();
}
private static async Task Process(IProgressBar progressBar)
{
using (var chil = progressBar.Spawn(TicksPerProcess, "Child Progress Bar"))
{
for (var i = 0; i < TicksPerProcess; i++)
{
progressBar.Tick();
chil.Tick();
await Task.Delay(1000);
}
}
}
With this method, I get all the child progress bars displayed, but all stuck on 10% and nothing is updating. Well I believe the task is running, but the console isn't updating.
As a test I threw in a for loop to update the main progress bar outside of the child task and that get's all of them updating with some flickering on the main progress bar, which says to me it's being updated from the child tasks as well. However that defeats the point of the main progress bar.
using (var pbar = new ProgressBar(NumberOfProcesses * TicksPerProcess, "Main Progress bar"))
{
for (int i = 0; i < NumberOfProcesses; i++)
{
Console.WriteLine($"Process: i");
var task = Process(pbar);
processes.Add(task);
}
for (int i = 0; i < NumberOfProcesses * TicksPerProcess; i++)
{
await Task.Delay(1000);
pbar.Tick();
}
}