Skip to content

Commit

Permalink
move to net6 for example project (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz authored Jun 7, 2022
1 parent 54d278c commit dd596e1
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace ShellProgressBar.Example.Examples
{
public class AlternateFinishedColorExample : ExampleBase
{
protected override void Start()
protected override async Task StartAsync()
{
var options = new ProgressBarOptions
{
Expand All @@ -17,23 +17,19 @@ protected override void Start()
BackgroundCharacter = '\u2593'
};

using (var pbar = new ProgressBar(100, "100 ticks", options))
{
Task.Run(
() =>
using var pbar = new ProgressBar(100, "100 ticks", options);
await Task.Run(
() =>
{
for (var i = 0; i < 10; i++)
{
for (var i = 0; i < 10; i++)
{
Task.Delay(10).Wait();
pbar.Tick($"Step {i}");
}
pbar.WriteErrorLine("The task ran into an issue!");
// OR pbar.ObservedError = true;
}).Wait();
pbar.Message= "Indicate the task is done, but the status is not Green.";
}

Task.Delay(5000).Wait();
Task.Delay(10).Wait();
pbar.Tick($"Step {i}");
}
pbar.WriteErrorLine("The task ran into an issue!");
// OR pbar.ObservedError = true;
});
pbar.Message= "Indicate the task is done, but the status is not Green.";
}


Expand Down
18 changes: 8 additions & 10 deletions src/ShellProgressBar.Example/Examples/ChildrenExample.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class ChildrenExample : ExampleBase
{
protected override void Start()
protected override Task StartAsync()
{
const int totalTicks = 10;
var options = new ProgressBarOptions
Expand All @@ -19,16 +20,13 @@ protected override void Start()
BackgroundColor = ConsoleColor.DarkGray,
ProgressCharacter = '─'
};
using (var pbar = new ProgressBar(totalTicks, "main progressbar", options))
using var pbar = new ProgressBar(totalTicks, "main progressbar", options);
TickToCompletion(pbar, totalTicks, sleep: 10, childAction: i =>
{
TickToCompletion(pbar, totalTicks, sleep: 10, childAction: i =>
{
using (var child = pbar.Spawn(totalTicks, "child actions", childOptions))
{
TickToCompletion(child, totalTicks, sleep: 100);
}
});
}
using var child = pbar.Spawn(totalTicks, "child actions", childOptions);
TickToCompletion(child, totalTicks, sleep: 100);
});
return Task.CompletedTask;
}
}
}
19 changes: 9 additions & 10 deletions src/ShellProgressBar.Example/Examples/ChildrenNoCollapseExample.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class ChildrenNoCollapseExample : ExampleBase
{
protected override void Start()
protected override Task StartAsync()
{
const int totalTicks = 10;
var options = new ProgressBarOptions
Expand All @@ -20,16 +21,14 @@ protected override void Start()
ProgressCharacter = '─',
CollapseWhenFinished = false
};
using (var pbar = new ProgressBar(totalTicks, "main progressbar", options))
using var pbar = new ProgressBar(totalTicks, "main progressbar", options);
TickToCompletion(pbar, totalTicks, sleep: 10, childAction: i =>
{
TickToCompletion(pbar, totalTicks, sleep: 10, childAction: i =>
{
using (var child = pbar.Spawn(totalTicks, "child actions", childOptions))
{
TickToCompletion(child, totalTicks, sleep: 100);
}
});
}
using var child = pbar.Spawn(totalTicks, "child actions", childOptions);
TickToCompletion(child, totalTicks, sleep: 100);
});

return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class DontDisplayInRealTimeExample : ExampleBase
{
protected override void Start()
protected override Task StartAsync()
{
const int totalTicks = 5;
var options = new ProgressBarOptions
Expand All @@ -13,6 +15,8 @@ protected override void Start()
{
TickToCompletion(pbar, totalTicks, sleep: 1750);
}

return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class DownloadProgressExample : ExampleBase
{
protected override void Start()
protected override Task StartAsync()
{
var files = new string[]
{
Expand All @@ -28,7 +29,11 @@ protected override void Start()
using var child = pbar.Spawn(100, "page: " + i, childOptions);
try
{
#pragma warning disable CS0618
#pragma warning disable SYSLIB0014
using var client = new WebClient();
#pragma warning restore CS0618
#pragma warning restore SYSLIB0014
client.DownloadProgressChanged += (o, args) => child.Tick(args.ProgressPercentage);
client.DownloadDataCompleted += (o, args) => data = args.Result;
client.DownloadDataAsync(new Uri(file));
Expand All @@ -44,6 +49,8 @@ protected override void Start()
pbar.WriteLine(error.Message);
}
}

return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class EstimatedDurationExample : ExampleBase
{
protected override void Start()
protected override Task StartAsync()
{
const int totalTicks = 10;
var options = new ProgressBarOptions
Expand All @@ -29,6 +30,8 @@ protected override void Start()
pbar.Tick(estimatedDuration, $"End {i + 1} of {totalTicks}: {initialMessage}");
}
}

return Task.CompletedTask;
}
}
}
7 changes: 3 additions & 4 deletions src/ShellProgressBar.Example/Examples/ExampleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ protected void TickToCompletion(IProgressBar pbar, int ticks, int sleep = 1750,
}
}

public Task Start(CancellationToken token)
public async Task Start(CancellationToken token)
{
RequestToQuit = false;
token.Register(() => RequestToQuit = true);

this.Start();
return Task.FromResult(1);
await this.StartAsync();
}

protected abstract void Start();
protected abstract Task StartAsync();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class FixedDurationExample : ExampleBase
{
protected override void Start()
protected override Task StartAsync()
{
var options = new ProgressBarOptions
{
Expand All @@ -24,6 +25,8 @@ protected override void Start()
Console.Error.WriteLine($"{nameof(FixedDurationBar)} did not signal {nameof(FixedDurationBar.CompletedHandle)} after {wait}");

}

return Task.CompletedTask;
}

private static void LongRunningTask(FixedDurationBar bar)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class IndeterminateChildrenNoCollapseExample : ExampleBase
{
protected override void Start()
protected override async Task StartAsync()
{
const int totalChildren = 10;
const int totalChildren = 2;
Random random = new Random();
var options = new ProgressBarOptions
{
Expand All @@ -29,7 +30,7 @@ protected override void Start()
pbar.Message = $"Start {i} of {totalChildren}: main progressbar";
using (var child = pbar.SpawnIndeterminate("child action " + i, childOptions))
{
Thread.Sleep(1000 * random.Next(5, 15));
await Task.Delay(1000 * random.Next(5, 15));
child.Finished();
}
pbar.Tick();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace ShellProgressBar.Example.Examples
{
public class IndeterminateProgressExample : ExampleBase
{
protected override void Start()
protected override Task StartAsync()
{
var options = new ProgressBarOptions
{
Expand All @@ -31,9 +31,7 @@ protected override void Start()
pbar.Message= "Finished! Moving on to the next in 5 seconds.";
}

Task.Delay(5000).Wait();
return Task.CompletedTask;
}


}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class MessageBeforeAndAfterExample : ExampleBase
{
protected override void Start()
protected override Task StartAsync()
{
Console.WriteLine("This should not be overwritten");
const int totalTicks = 10;
Expand All @@ -24,6 +25,7 @@ protected override void Start()
}

Console.WriteLine("This should not be overwritten either afterwards");
return Task.CompletedTask;
}
}
}
21 changes: 12 additions & 9 deletions src/ShellProgressBar.Example/Examples/PersistMessageExample.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class PersistMessageExample : ExampleBase
{
protected override void Start()
protected override Task StartAsync()
{
var options = new ProgressBarOptions
{
ForegroundColor = ConsoleColor.Yellow,
ForegroundColorDone = ConsoleColor.DarkGreen,
ForegroundColorError = ConsoleColor.Red,
BackgroundColor = ConsoleColor.DarkGray,
BackgroundCharacter = '\u2593',
WriteQueuedMessage = o =>
Expand All @@ -32,23 +34,24 @@ protected override void Start()
}
};
var wait = TimeSpan.FromSeconds(6);
using (var pbar = new FixedDurationBar(wait, "", options))
{
var t = new Thread(()=> LongRunningTask(pbar));
t.Start();

if (!pbar.CompletedHandle.WaitOne(wait.Subtract(TimeSpan.FromSeconds(.5))))
pbar.WriteErrorLine($"{nameof(FixedDurationBar)} did not signal {nameof(FixedDurationBar.CompletedHandle)} after {wait}");
using var pbar = new FixedDurationBar(wait, "", options);
var t = new Thread(()=> LongRunningTask(pbar));
t.Start();

if (!pbar.CompletedHandle.WaitOne(wait.Subtract(TimeSpan.FromSeconds(.5))))
{
pbar.WriteErrorLine($"{nameof(FixedDurationBar)} did not signal {nameof(FixedDurationBar.CompletedHandle)} after {wait}");
pbar.Dispose();
}
return Task.CompletedTask;
}

private static void LongRunningTask(FixedDurationBar bar)
{
for (var i = 0; i < 1_000_000; i++)
{
bar.Message = $"{i} events";
if (bar.IsCompleted) break;
if (bar.IsCompleted || bar.ObservedError) break;
if (i % 500 == 0) bar.WriteLine($"Report {i} to console above the progressbar");
Thread.Sleep(1);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using System;
using System.Threading.Tasks;

namespace ShellProgressBar.Example.Examples
{
public class ProgressBarOnBottomExample : ExampleBase
{
protected override void Start()
protected override Task StartAsync()
{
const int totalTicks = 10;
var options = new ProgressBarOptions
{
ProgressCharacter = '─',
ProgressBarOnBottom = true
};
using (var pbar = new ProgressBar(totalTicks, "progress bar is on the bottom now", options))
{
TickToCompletion(pbar, totalTicks, sleep: 500);
}
using var pbar = new ProgressBar(totalTicks, "progress bar is on the bottom now", options);
TickToCompletion(pbar, totalTicks, sleep: 500);

return Task.CompletedTask;
}
}
}
Loading

0 comments on commit dd596e1

Please sign in to comment.