-
Notifications
You must be signed in to change notification settings - Fork 136
/
PersistMessageExample.cs
60 lines (56 loc) · 1.78 KB
/
PersistMessageExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ShellProgressBar.Example.Examples
{
public class PersistMessageExample : ExampleBase
{
protected override Task StartAsync()
{
var options = new ProgressBarOptions
{
ForegroundColor = ConsoleColor.Yellow,
ForegroundColorDone = ConsoleColor.DarkGreen,
ForegroundColorError = ConsoleColor.Red,
BackgroundColor = ConsoleColor.DarkGray,
BackgroundCharacter = '\u2593',
WriteQueuedMessage = o =>
{
var writer = o.Error ? Console.Error : Console.Out;
var c = o.Error ? ConsoleColor.DarkRed : ConsoleColor.Blue;
if (o.Line.StartsWith("Report 500"))
{
Console.ForegroundColor = ConsoleColor.Yellow;
writer.WriteLine("Add an extra message, because why not");
Console.ForegroundColor = c;
writer.WriteLine(o.Line);
return 2; //signal to the progressbar we wrote two messages
}
Console.ForegroundColor = c;
writer.WriteLine(o.Line);
return 1;
}
};
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}");
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 || bar.ObservedError) break;
if (i % 500 == 0) bar.WriteLine($"Report {i} to console above the progressbar");
Thread.Sleep(1);
}
}
}
}