From 4f1e5abaa2ee28132aaa3295021ce28afabc8ee0 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 7 Jun 2022 20:42:23 +0200 Subject: [PATCH] move to net6 for example project --- .../Examples/AlternateFinishedColorExample.cs | 30 ++++++++----------- .../Examples/ChildrenExample.cs | 18 +++++------ .../Examples/ChildrenNoCollapseExample.cs | 19 ++++++------ .../Examples/DontDisplayInRealTimeExample.cs | 6 +++- .../Examples/DownloadPogressExample.cs | 9 +++++- .../Examples/EstimatedDurationExample.cs | 5 +++- .../Examples/ExampleBase.cs | 7 ++--- .../Examples/FixedDurationExample.cs | 5 +++- .../IndeterminateChildrenNoCollapse.cs | 7 +++-- .../Examples/IndeterminateProgressExample.cs | 6 ++-- .../Examples/MessageBeforeAndAfterExample.cs | 4 ++- .../Examples/PersistMessageExample.cs | 21 +++++++------ .../Examples/ProgressBarOnBottomExample.cs | 11 +++---- .../Examples/StylingExample.cs | 15 +++++----- src/ShellProgressBar.Example/Program.cs | 17 +++++------ .../ShellProgressBar.Example.csproj | 2 +- .../ShellProgressBar.Tests.csproj | 2 +- 17 files changed, 98 insertions(+), 86 deletions(-) diff --git a/src/ShellProgressBar.Example/Examples/AlternateFinishedColorExample.cs b/src/ShellProgressBar.Example/Examples/AlternateFinishedColorExample.cs index d4bae7a..6614a8f 100644 --- a/src/ShellProgressBar.Example/Examples/AlternateFinishedColorExample.cs +++ b/src/ShellProgressBar.Example/Examples/AlternateFinishedColorExample.cs @@ -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 { @@ -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."; } diff --git a/src/ShellProgressBar.Example/Examples/ChildrenExample.cs b/src/ShellProgressBar.Example/Examples/ChildrenExample.cs index 2f7ff1e..954dbfc 100644 --- a/src/ShellProgressBar.Example/Examples/ChildrenExample.cs +++ b/src/ShellProgressBar.Example/Examples/ChildrenExample.cs @@ -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 @@ -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; } } } diff --git a/src/ShellProgressBar.Example/Examples/ChildrenNoCollapseExample.cs b/src/ShellProgressBar.Example/Examples/ChildrenNoCollapseExample.cs index 1a0192b..4e231b7 100644 --- a/src/ShellProgressBar.Example/Examples/ChildrenNoCollapseExample.cs +++ b/src/ShellProgressBar.Example/Examples/ChildrenNoCollapseExample.cs @@ -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 @@ -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; } } } diff --git a/src/ShellProgressBar.Example/Examples/DontDisplayInRealTimeExample.cs b/src/ShellProgressBar.Example/Examples/DontDisplayInRealTimeExample.cs index decb569..b28f0aa 100644 --- a/src/ShellProgressBar.Example/Examples/DontDisplayInRealTimeExample.cs +++ b/src/ShellProgressBar.Example/Examples/DontDisplayInRealTimeExample.cs @@ -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 @@ -13,6 +15,8 @@ protected override void Start() { TickToCompletion(pbar, totalTicks, sleep: 1750); } + + return Task.CompletedTask; } } } diff --git a/src/ShellProgressBar.Example/Examples/DownloadPogressExample.cs b/src/ShellProgressBar.Example/Examples/DownloadPogressExample.cs index d9f8f44..d535aaa 100644 --- a/src/ShellProgressBar.Example/Examples/DownloadPogressExample.cs +++ b/src/ShellProgressBar.Example/Examples/DownloadPogressExample.cs @@ -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[] { @@ -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)); @@ -44,6 +49,8 @@ protected override void Start() pbar.WriteLine(error.Message); } } + + return Task.CompletedTask; } } } diff --git a/src/ShellProgressBar.Example/Examples/EstimatedDurationExample.cs b/src/ShellProgressBar.Example/Examples/EstimatedDurationExample.cs index c0fd8e6..a7aa795 100644 --- a/src/ShellProgressBar.Example/Examples/EstimatedDurationExample.cs +++ b/src/ShellProgressBar.Example/Examples/EstimatedDurationExample.cs @@ -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 @@ -29,6 +30,8 @@ protected override void Start() pbar.Tick(estimatedDuration, $"End {i + 1} of {totalTicks}: {initialMessage}"); } } + + return Task.CompletedTask; } } } diff --git a/src/ShellProgressBar.Example/Examples/ExampleBase.cs b/src/ShellProgressBar.Example/Examples/ExampleBase.cs index fae2653..eb81e2b 100644 --- a/src/ShellProgressBar.Example/Examples/ExampleBase.cs +++ b/src/ShellProgressBar.Example/Examples/ExampleBase.cs @@ -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(); } } diff --git a/src/ShellProgressBar.Example/Examples/FixedDurationExample.cs b/src/ShellProgressBar.Example/Examples/FixedDurationExample.cs index f316faf..5293f9f 100644 --- a/src/ShellProgressBar.Example/Examples/FixedDurationExample.cs +++ b/src/ShellProgressBar.Example/Examples/FixedDurationExample.cs @@ -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 { @@ -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) diff --git a/src/ShellProgressBar.Example/Examples/IndeterminateChildrenNoCollapse.cs b/src/ShellProgressBar.Example/Examples/IndeterminateChildrenNoCollapse.cs index 8169016..dfb0acf 100644 --- a/src/ShellProgressBar.Example/Examples/IndeterminateChildrenNoCollapse.cs +++ b/src/ShellProgressBar.Example/Examples/IndeterminateChildrenNoCollapse.cs @@ -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 { @@ -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(); diff --git a/src/ShellProgressBar.Example/Examples/IndeterminateProgressExample.cs b/src/ShellProgressBar.Example/Examples/IndeterminateProgressExample.cs index 3573200..8bf265a 100644 --- a/src/ShellProgressBar.Example/Examples/IndeterminateProgressExample.cs +++ b/src/ShellProgressBar.Example/Examples/IndeterminateProgressExample.cs @@ -6,7 +6,7 @@ namespace ShellProgressBar.Example.Examples { public class IndeterminateProgressExample : ExampleBase { - protected override void Start() + protected override Task StartAsync() { var options = new ProgressBarOptions { @@ -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; } - - } } diff --git a/src/ShellProgressBar.Example/Examples/MessageBeforeAndAfterExample.cs b/src/ShellProgressBar.Example/Examples/MessageBeforeAndAfterExample.cs index 027dd5e..4280eef 100644 --- a/src/ShellProgressBar.Example/Examples/MessageBeforeAndAfterExample.cs +++ b/src/ShellProgressBar.Example/Examples/MessageBeforeAndAfterExample.cs @@ -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; @@ -24,6 +25,7 @@ protected override void Start() } Console.WriteLine("This should not be overwritten either afterwards"); + return Task.CompletedTask; } } } diff --git a/src/ShellProgressBar.Example/Examples/PersistMessageExample.cs b/src/ShellProgressBar.Example/Examples/PersistMessageExample.cs index a2470c5..2af136b 100644 --- a/src/ShellProgressBar.Example/Examples/PersistMessageExample.cs +++ b/src/ShellProgressBar.Example/Examples/PersistMessageExample.cs @@ -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 => @@ -32,15 +34,16 @@ 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) @@ -48,7 +51,7 @@ 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); } diff --git a/src/ShellProgressBar.Example/Examples/ProgressBarOnBottomExample.cs b/src/ShellProgressBar.Example/Examples/ProgressBarOnBottomExample.cs index e48d68d..83733a4 100644 --- a/src/ShellProgressBar.Example/Examples/ProgressBarOnBottomExample.cs +++ b/src/ShellProgressBar.Example/Examples/ProgressBarOnBottomExample.cs @@ -1,10 +1,11 @@ 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 @@ -12,10 +13,10 @@ protected override void Start() 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; } } } diff --git a/src/ShellProgressBar.Example/Examples/StylingExample.cs b/src/ShellProgressBar.Example/Examples/StylingExample.cs index c2d8291..f22711b 100644 --- a/src/ShellProgressBar.Example/Examples/StylingExample.cs +++ b/src/ShellProgressBar.Example/Examples/StylingExample.cs @@ -1,10 +1,11 @@ using System; +using System.Threading.Tasks; namespace ShellProgressBar.Example.Examples { public class StylingExample : ExampleBase { - protected override void Start() + protected override Task StartAsync() { const int totalTicks = 10; var options = new ProgressBarOptions @@ -14,13 +15,13 @@ protected override void Start() BackgroundColor = ConsoleColor.DarkGray, BackgroundCharacter = '\u2593' }; - using (var pbar = new ProgressBar(totalTicks, "showing off styling", options)) + using var pbar = new ProgressBar(totalTicks, "showing off styling", options); + TickToCompletion(pbar, totalTicks, sleep: 500, i => { - TickToCompletion(pbar, totalTicks, sleep: 500, i => - { - if (i > 5) pbar.ForegroundColor = ConsoleColor.Red; - }); - } + if (i > 5) pbar.ForegroundColor = ConsoleColor.Red; + }); + + return Task.CompletedTask; } } } diff --git a/src/ShellProgressBar.Example/Program.cs b/src/ShellProgressBar.Example/Program.cs index 264721d..b947538 100644 --- a/src/ShellProgressBar.Example/Program.cs +++ b/src/ShellProgressBar.Example/Program.cs @@ -12,7 +12,6 @@ class Program { private static readonly IList TestCases = new List { - /* new PersistMessageExample(), new FixedDurationExample(), new DeeplyNestedProgressBarTreeExample(), @@ -29,7 +28,6 @@ class Program new EstimatedDurationExample(), new IndeterminateProgressExample(), new IndeterminateChildrenNoCollapseExample(), - */ new AlternateFinishedColorExample() }; @@ -49,19 +47,18 @@ class Program new AlternateFinishedColorExample() }; - static void Main(string[] args) + public static async Task Main(string[] args) { - Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); var cts = new CancellationTokenSource(); Console.CancelKeyPress += (s, e) => { cts.Cancel(); }; - MainAsync(args, cts.Token).GetAwaiter().GetResult(); + await MainAsync(args, cts.Token); } - static async Task MainAsync(string[] args, CancellationToken token) + private static async Task MainAsync(string[] args, CancellationToken token) { var command = args.Length > 0 ? args[0] : "test"; switch (command) @@ -71,19 +68,19 @@ static async Task MainAsync(string[] args, CancellationToken token) return; case "example": var nth = args.Length > 1 ? int.Parse(args[1]) : 0; - await RunExample(token, nth); + await RunExample(nth, token); return; default: - Console.Error.WriteLine($"Unknown command:{command}"); + await Console.Error.WriteLineAsync($"Unknown command:{command}"); return; } } - private static async Task RunExample(CancellationToken token, int nth) + private static async Task RunExample(int nth, CancellationToken token) { if (nth > Examples.Count - 1 || nth < 0) { - Console.Error.WriteLine($"There are only {Examples.Count} examples, {nth} is not valid"); + await Console.Error.WriteLineAsync($"There are only {Examples.Count} examples, {nth} is not valid"); } var example = Examples[nth]; diff --git a/src/ShellProgressBar.Example/ShellProgressBar.Example.csproj b/src/ShellProgressBar.Example/ShellProgressBar.Example.csproj index ea4e6d5..e4b427d 100644 --- a/src/ShellProgressBar.Example/ShellProgressBar.Example.csproj +++ b/src/ShellProgressBar.Example/ShellProgressBar.Example.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp2.1 + net6.0 shellprogressbar-example ShellProgressBar.Example $(CurrentVersion) diff --git a/test/ShellProgressBar.Tests/ShellProgressBar.Tests.csproj b/test/ShellProgressBar.Tests/ShellProgressBar.Tests.csproj index b9e3401..ae86f2a 100644 --- a/test/ShellProgressBar.Tests/ShellProgressBar.Tests.csproj +++ b/test/ShellProgressBar.Tests/ShellProgressBar.Tests.csproj @@ -1,7 +1,7 @@ - net5.0 + net6.0 false