public static void Main(string[] args) { Loop.Default.Run(async() => { var jobs = new LinkedList <Job>(); var stdin = new TTY(0); string str = null; while ((str = await stdin.ReadStringAsync()) != null) { str = str.TrimEnd(new char[] { '\r', '\n' }); if (str == "quit") { Loop.Current.Stop(); break; } else if (str.StartsWith("fib ")) { int n; if (!int.TryParse(str.Substring("fib ".Length), out n)) { Console.WriteLine("Supply an integer to the fib command"); continue; } TrackCalculateFibonacci(jobs, n); } else if (str == "help") { Console.WriteLine("Available commands: "); Console.WriteLine("fib <n:int> - start a thread which calculates fib"); Console.WriteLine("help - displays help"); Console.WriteLine("quit - quits the program"); } else if (str == "count") { Console.WriteLine("Total jobs: {0}", jobs.Count); int i = 0; foreach (var job in jobs) { var awaiter = job.Task.GetAwaiter(); Console.WriteLine("{0}: {1} {2} {3}", i, job.StartTime, job.TimeRunning, job.Task.Status); i++; } } else { Console.WriteLine("Unknown command"); } } }); }
public static void Main(string[] args) { Loop.Default.Run(async() => { LinkedList <Task> jobs = new LinkedList <Task>(); var stdin = new TTY(0); string str = null; while ((str = await stdin.ReadStringAsync()) != null) { str = str.TrimEnd(new char[] { '\r', '\n' }); if (str == "quit") { break; } else if (str.StartsWith("fib ")) { int n; if (!int.TryParse(str.Substring("fib ".Length), out n)) { Console.WriteLine("Supply an integer to the fib command"); return; } var task = CalculateFibonacci(n); var node = jobs.AddLast(task); task.ContinueWith((_) => jobs.Remove(node)); } else if (str == "help") { Console.WriteLine("Available commands: "); Console.WriteLine("fib <n:int> - start a thread which calculates fib"); Console.WriteLine("help - displays help"); Console.WriteLine("quit - quits the program"); } else if (str == "count") { Console.WriteLine("Total jobs: {0}", jobs.Count); } else { Console.WriteLine("Unknown command"); } } }); }