示例#1
0
        static void Main(string[] args)
        {
            char      input;
            double    elapsedTime = 0.0;
            DateTime  startTime = new DateTime(), endTime = new DateTime();
            Stopwatch Timer = new Stopwatch();

            Timer.Controls();
            input = Console.ReadKey().KeyChar;
            Console.WriteLine("\n");

            do
            {
                switch (input)
                {
                case 's':     //start/resume the clock
                    if (Timer.GetState == false && Timer.GetFirstTime == true)
                    {
                        Timer.SetStart();
                        Timer.SetDisplay(false);
                        startTime = Timer.Capture();
                        Console.WriteLine("Clock has started\n");
                    }
                    else if (Timer.GetState == false && Timer.GetFirstTime == false)
                    {
                        Timer.SetStart();
                        Timer.SetDisplay(false);
                        startTime = Timer.Capture();
                        Console.WriteLine("Clock has resumed\n");
                    }
                    else
                    {
                        //convert to proper exception handling
                        Console.WriteLine("Clock is already running\n\n");
                    }
                    break;

                case 'e':     //stop the clock
                    if (Timer.GetState == true)
                    {
                        Timer.SetStop();
                        endTime = Timer.Capture();
                        Console.WriteLine("Time captured\n");
                    }
                    else
                    {
                        //convert to proper exception handling
                        Console.WriteLine("Clock must be started again\n\n");
                    }
                    break;

                case 'd':     //display elapsed time
                    if (Timer.GetState == false && Timer.GetIsDisplayed == false)
                    {
                        elapsedTime += Timer.ElapsedTime(endTime, startTime);
                        Console.WriteLine("Elapsed time is " + elapsedTime.ToString("#.00") + " seconds\n");
                        Timer.SetDisplay(true);
                    }
                    else if (Timer.GetState == false && Timer.GetIsDisplayed == true)
                    {
                        Console.WriteLine("Again.....the elapsed time is " + elapsedTime.ToString("#.00") + " seconds\n");
                    }
                    else
                    {
                        //convert to proper exception handling
                        Console.WriteLine("Clock is still running. Please stop the clock and try again\n");
                    }
                    break;

                case 'r':     //reset the clock
                    Timer.SetStop();
                    Timer.Reset(ref elapsedTime);
                    startTime = endTime = new DateTime();
                    Console.WriteLine("Clock has been reset to 0.00 seconds\n");
                    break;

                case 'q':     //end the simulation
                    break;

                default:     //wrong command
                    Console.WriteLine("Invalid Command\n");
                    break;
                }

                if (input != 'q')
                {
                    Timer.Controls();
                    input = Console.ReadKey().KeyChar;
                    Console.WriteLine("\n");
                }
            } while (input != 'q');

            Console.WriteLine("Ending Simulation");
        }