static void RunClock() { var con = new ThreadsafeWriter(); var x = con.Width - 10; // not the best way to signal to thread to stop, will do for demo while (!stopClock) { con.PrintAt(x, 0, DateTime.Now.ToString("HH:MM:ss")); Thread.Sleep(1000); } }
static void Seconds(int y) { var sw = new Stopwatch(); sw.Start(); var con = new ThreadsafeWriter(); // not the best way to signal to thread to stop, will do for demo while (!stopClock) { decimal seconds = sw.ElapsedMilliseconds / 1000M; con.PrintAtColor(ConsoleColor.Red, 17, y + 1, $"{seconds:0.00}"); Thread.Sleep(100); } }
static void ProcessFakeFiles() { var con = new ThreadsafeWriter(); // Screen will have been cleared so we are at the top // write one empty line so that the first progress bar does not // overlap with the demo clock. con.WriteLine(""); int numDirs = 15; con.ForegroundColor = ConsoleColor.White; var r = new Random(); var dirs = Files.Take(numDirs).Select(f => f.Split(new[] { '.' })[0]); var tasks = new List <Task>(); var bars = new List <ProgressBar>(); int cnt = dirs.Count(); foreach (var d in dirs) { var dir = d; var files = Files.Take(r.Next(30) + 10).ToArray(); var bar = new ProgressBar(files.Count()); bars.Add(bar); bar.Refresh(0, d); tasks.Add(new Task(() => ProcessFakeFiles(d, files, bar))); } int y = bars.Last().Y + 1; con.WriteLine("Press any key to start"); Console.ReadKey(true); con.WriteLine("processing... "); var seconds = Task.Run(() => Seconds(y)); foreach (var t in tasks) { t.Start(); } Task.WaitAll(tasks.ToArray()); stopClock = true; seconds.Wait(); con.WriteLine(ConsoleColor.Yellow, "finished. "); }