コード例 #1
0
ファイル: Fibonacci.cs プロジェクト: txdv/LibuvSharp
        public static void Main(string[] args)
        {
            var stdin = new TTY(0);

            stdin.Read(Encoding.ASCII, (str) => {
                str = str.TrimEnd(new char[] { '\r', '\n' });
                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;
                    }
                    TimeSpan span  = TimeSpan.Zero;
                    BigInteger res = 0;
                    Console.WriteLine("{0}: fib({1}) starting", span, n);
                    Loop.Default.QueueUserWorkItem(() => {
                        var stopwatch = Stopwatch.StartNew();
                        res           = Fibonacci(n);
                        stopwatch.Stop();
                        span = stopwatch.Elapsed;
                    }, () => {
                        Console.WriteLine("{0}: fib({1}) = {2}", span, n, res);
                    });
                }
                else if (str == "quit")
                {
                    Loop.Default.Stop();
                    stdin.Close();
                }
                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
                {
                    Console.WriteLine("Unknown command");
                }
            });
            stdin.Resume();
            Loop.Default.Run();
        }