-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FixedDurationBar subclass of ProgressBar for the cases where you …
…want to display a progress with a well known fixed duration
- Loading branch information
Showing
6 changed files
with
100 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/ShellProgressBar.Example/Examples/FixedDurationExample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Threading; | ||
|
||
namespace ShellProgressBar.Example.Examples | ||
{ | ||
public class FixedDurationExample : ExampleBase | ||
{ | ||
protected override void Start() | ||
{ | ||
var options = new ProgressBarOptions | ||
{ | ||
ForegroundColor = ConsoleColor.Yellow, | ||
ForegroundColorDone = ConsoleColor.DarkGreen, | ||
BackgroundColor = ConsoleColor.DarkGray, | ||
BackgroundCharacter = '\u2593' | ||
}; | ||
var wait = TimeSpan.FromSeconds(25); | ||
using (var pbar = new FixedDurationBar(wait, "", options)) | ||
{ | ||
var t = new Thread(()=> LongRunningTask(pbar)); | ||
t.Start(); | ||
|
||
if (!pbar.CompletedHandle.WaitOne(wait)) | ||
Console.Error.WriteLine($"{nameof(FixedDurationBar)} did not signal {nameof(FixedDurationBar.CompletedHandle)} after {wait}"); | ||
|
||
} | ||
} | ||
|
||
private static void LongRunningTask(FixedDurationBar bar) | ||
{ | ||
for (var i = 0; i < 1_000_000; i++) | ||
{ | ||
bar.Message = $"{i} events"; | ||
if (bar.IsCompleted) break; | ||
Thread.Sleep(1); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Threading; | ||
|
||
namespace ShellProgressBar | ||
{ | ||
public class FixedDurationBar : ProgressBar | ||
{ | ||
public bool IsCompleted { get; private set; } | ||
|
||
private readonly ManualResetEvent _completedHandle = new ManualResetEvent(false); | ||
public WaitHandle CompletedHandle => _completedHandle; | ||
|
||
public FixedDurationBar(TimeSpan duration, string message, ConsoleColor color) : this(duration, message, new ProgressBarOptions {ForegroundColor = color}) { } | ||
|
||
public FixedDurationBar(TimeSpan duration, string message, ProgressBarOptions options = null) : base((int)Math.Ceiling(duration.TotalSeconds), message, options) | ||
{ | ||
if (!this.Options.DisplayTimeInRealTime) | ||
throw new ArgumentException( | ||
$"{nameof(ProgressBarOptions)}.{nameof(ProgressBarOptions.DisplayTimeInRealTime)} has to be true for {nameof(FixedDurationBar)}", nameof(options) | ||
); | ||
} | ||
|
||
private long _seenTicks = 0; | ||
protected override void OnTimerTick() | ||
{ | ||
Interlocked.Increment(ref _seenTicks); | ||
if (_seenTicks % 2 == 0) this.Tick(); | ||
base.OnTimerTick(); | ||
} | ||
|
||
protected override void OnDone() | ||
{ | ||
this.IsCompleted = true; | ||
this._completedHandle.Set(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters