コード例 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("--Calculate a Fibonacci number at a certain position (0-49)--");
            char answer;

            do
            {
                Console.Write("\nEnter a position to compute: ");
                try
                {
                    int num = Convert.ToInt32(Console.ReadLine());
                    if (num < 0 || num > 49)
                    {
                        Console.WriteLine("ERROR: Position should be an integer from 0 to 49.");
                    }
                    else
                    {
                        Calculate    c         = new Calculate(Fibonacci);
                        IAsyncResult iasResult = c.BeginInvoke(num, null, null);

                        // This message will keep printing until
                        // the Fibonacci() method is finished.
                        while (!iasResult.IsCompleted)
                        {
                            Console.WriteLine("Main() invoked on thread {0}.",
                                              Thread.CurrentThread.ManagedThreadId);
                            Console.WriteLine("continuing to do work...");
                            Thread.Sleep(1000);
                        }

                        int result = c.EndInvoke(iasResult);
                        Console.WriteLine($"Fibonacci value at position {num}: {result}");
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("ERROR: A position should be an integer.");
                }
                finally
                {
                    Console.Write("Do you want to continue? (Y or N) ");
                    answer = Convert.ToChar(Console.ReadLine());
                }
            } while (answer == 'y' || answer == 'Y');

            Console.WriteLine("\nProgram Ended");

            Console.WriteLine("\nPress <Enter> to quit...");
            Console.ReadKey();
        }